Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0120 error under vs2010 beta 2 - object reference is required

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
like image 524
Robert Ivanc Avatar asked Oct 21 '09 10:10

Robert Ivanc


1 Answers

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.

like image 62
Marc Gravell Avatar answered Oct 09 '22 01:10

Marc Gravell