简体   繁体   English

Mono C#Compiler和MS C#Compiler关于Scope的区别

[英]The difference between Mono C# Compiler and MS C# Compiler Regarding Scope

I'm running in a corner case here with regarding the difference with scoping of instance methods/properties in C#. 我在这里讨论与C#中实例方法/属性的作用域的差异。 Here is the code: 这是代码:

public class Base
{
   public EventHandler Click {get;set;}
   public Base(EventHandler clickHandler)
   {
      this.Click = clickHandler;
   }
}

public class Derived: Base
{
   public Derived(): base((sender, e) => Execute())
   {
   }

   private void Execute()
   {
   }
}

The code compiles fine on MonoDevelop 3.0, but gives an error in VS2010 saying: An object reference is required for the non-static field, method, or property "Base.Execute" Basically, it boils down to the fact that when calling base class's constructor from derived class's constructor, MS's C# compiler does not allow access to derived class's methods/properties, etc. How so? 代码在MonoDevelop 3.0上编译得很好,但在VS2010中给出了一个错误:非静态字段,方法或属性需要对象引用“Base.Execute”基本上,它归结为调用基类时的事实构造函数来自派生类的构造函数,MS的C#编译器不允许访问派生类的方法/属性等。如何?

The VS compiler follows the specification. VS编译器遵循规范。 Not sure what is the reason it is allowed in Mono implemetation. 不确定在Mono实现中允许它的原因是什么。

C# Specification , section 10.11.1 Constructor initializers: C#规范 ,第10.11.1节构造函数初始值设定项:

An instance constructor initializer cannot access the instance being created. 实例构造函数初始值设定项无法访问正在创建的实例。 Therefore it is a compile-time error to reference this in an argument expression of the constructor initializer, as is it a compile-time error for an argument expression to reference any instance member through a simple-name. 因此,在构造函数初始值设定项的参数表达式中引用它是一个编译时错误,因为参数表达式通过简单名称引用任何实例成员的编译时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM