Edit

Share via


Compiler Warning (level 1) CS1911

Access to member 'name' through a 'base' keyword from an anonymous method, lambda expression, query expression, or iterator results in unverifiable code. Consider moving the access into a helper method on the containing type.

Calling virtual functions with the base keyword inside the method body of an iterator or anonymous methods will result in unverifiable code. Unverifiable code will fail to run in a partial trust environment.

One resolution for CS1911 is to move the virtual function call to a helper function.

Example

The following sample generates CS1911.

// CS1911.cs  
// compile with: /W:1  
using System;  
  
delegate void D();  
delegate D RetD();  
  
class B {  
   protected virtual void M() {  
      Console.WriteLine("B.M");  
   }  
}  
  
class Der : B {  
   protected override void M() {  
      Console.WriteLine("D.M");  
   }  
  
   void Test() { base.M(); }  
   D Test2() { return new D(base.M); }  
  
   public D CallBaseM() {  
      return delegate () { base.M(); };   // CS1911  
  
      // try the following line instead  
      // return delegate () { Test(); };  
   }  
  
   public RetD DelToBaseM() {  
      return delegate () { return new D(base.M); };   // CS1911  
  
      // try the following line instead  
      // return delegate () { return Test2(); };  
   }  
}  
  
class Program {  
   public static void Main() {  
      Der der = new Der();  
      D d = der.CallBaseM();  
      d();  
      RetD rd = der.DelToBaseM();  
      rd()();  
   }  
}