在VS2010 Beta 2的CS0120错误 - 对象引用是必需的 [英] CS0120 error under vs2010 beta 2 - object reference is required

查看:172
本文介绍了在VS2010 Beta 2的CS0120错误 - 对象引用是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用VS2008做工精细下面的代码:

 命名空间N2.Engine.Globalization 
{
公共类DictionaryScope:适用范围
{
对象PREVIOUSVALUE;
公共DictionaryScope(IDictionary的字典,对象键,对象的值)
:基地(代表
{

如果(dictionary.Contains(密钥))
PREVIOUSVALUE =词典[关键];
字典[关键] =价值;
},代表
{
如果(PREVIOUSVALUE == NULL)
dictionary.Remove(关键);
,否则
字典[关键] = PREVIOUSVALUE;
})
{

}
}
}

但现在报道的对象引用是必需的非静态字段,方法或属性N2。 Engine.Globalization.DictionaryScope.previousValue



好像什么东西在编译器改变了吗?任何变通办法?



更​​新:



关于使用虚拟方法的建议。这可能不会告发工作或者,作为虚拟方法会得到从基类的构造,我相信也不可能叫什么名字?



下面是范围(基类)的实现:

 公共类范围:IDisposable的
{
结束行动;

公开范围(动作开始,动作结束)
{
开头();
this.end =结束;
}

公共无效结束()
{
端();
}

#地区IDisposable的会员

无效IDisposable.Dispose()
{
端();
}

#endregion


解决方案

更新:




第7.5.7此访问



A 此访问的由保留字这个



此访问:

 

A 此访问仅在的实例构造函数,一个实例方法,或实例访问。




这是没有这些。 4.0编译器看起来是正确的。想必这是不是高兴,因为这的在本质上的规定在一个点上访问这个时,未初始化的类型。也许;-p



请注意,我期望它是不是真正的 this.someField 导致此 - 更多的的使用的领域的原因这个抓获的,这意味着它要扯起这个实例到一个编译器生成的类 - 就好像你这样写:

 公共MyCtor ():基地(新SOMETYPE(本).SomeMethod){...} 

在C#3.0编译斑点这个





$ b的上述滥用$ b

转载。调查。它看起来像解决隐性这个在构造函数链的问题。



最有可能的解决方法是使用一个虚拟方法,而不是代表,并简单地将其覆盖在派生类中。



一个解决方法是将PAS作为参数,所以委托变为OBJ => obj.whatever ...,并使用实例 theDelegate(本);



简单摄制:

 公共类MyBase {
公共MyBase(动作一){}
}
公共类MySub:MyBase {
私人字符串foo的; 这个
//使用,说使用无效的本
//不说,比如需要
公共MySub()这个:基地(委托{this.foo =ABC;}){}
}

我需要检查规范,但我不知道是否这个 在这种情况下......有效的让4.0编译器可能是正确的。


the following code used to work fine under vs2008:

namespace N2.Engine.Globalization
{
    public class DictionaryScope : Scope
    {
                object previousValue;
    	public DictionaryScope(IDictionary dictionary, object key, object value)
    		: base(delegate
    		{

    			if (dictionary.Contains(key))
    				previousValue = dictionary[key];
    			dictionary[key] = value;
    		}, delegate
    		{
    			if (previousValue == null)
    				dictionary.Remove(key);
    			else
    				dictionary[key] = previousValue;
    		})
    	{

    	}
    }
}

but now it reports An object reference is required for the non-static field, method, or property 'N2.Engine.Globalization.DictionaryScope.previousValue'

It seems something changed in the compiler? Any workarounds?

update:

regarding the suggestion to use a virtual method. This probably wouldn work either, as the virtual method would get called from the base constructor, which I believe is also not possible?

Here is the implementation of the Scope (base class):

public class Scope: IDisposable
    {
    	Action end;

    	public Scope(Action begin, Action end)
    	{
    		begin();
    		this.end = end;
    	}

    	public void End()
    	{
    		end();
    	}

    	#region IDisposable Members

    	void IDisposable.Dispose()
    	{
    		End();
    	}

    	#endregion

解决方案

Update:

§ 7.5.7 This access

A this-access consists of the reserved word this.

this-access:

this

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor.

This is none of these. The 4.0 compiler looks to be correct. Presumably it isn't happy because this in essence provides access to this at a point when the type isn't initialized. Maybe ;-p

Note that I expect that it isn't really the this.someField that causes this - more that the use of a field causes this to be captured, meaning it wants to hoist the this instance onto a compiler-generated class - as though you had written:

public MyCtor() : base( new SomeType(this).SomeMethod ) {...}

The C# 3.0 compiler spots the above abuse of this.


Reproduced. Investigating. It looks like an issue resolving the implicit this in the constructor chaining.

The most likely workaround would be to use a virtual method instead of a delegate, and simply override it in the derived class.

One workaround would be to pas the instance in as an argument, so the delegate becomes "obj => obj.whatever...", and use theDelegate(this);.

Simpler repro:

public class MyBase {
    public MyBase(Action a) { }
}
public class MySub : MyBase {
    private string foo;
    // with "this.", says invalid use of "this"
    // without "this.", says instance required
    public MySub() : base(delegate { this.foo = "abc"; }) { }
}

I would need to check the spec, but I'm not sure whether this is valid in this context... so the 4.0 compiler could be correct.

这篇关于在VS2010 Beta 2的CS0120错误 - 对象引用是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆