开发者

Lambda expression type inference is different in inheritance chain. Why?

Given the following classes:

public class Class1<TObject> {
    protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref TType store, TType value) {
    }
}

public class Class2<TObject> : Class1<Class2<TObject>>{
    private int _propertyInt;
    public int PropertyInt {
        get { return _propertyInt; }
        set { MethodA(c2 => c2.PropertyInt, ref _propertyInt, value); }
    }
}

public class Class3 : Class2<Class3> {
    private float _propertyFloat;
    public float PropertyFloat {
        get { return _propertyFloat; }
        set {开发者_C百科 MethodA(c3 => c3.PropertyFloat, ref _propertyFloat, value); }
    }
}

For Class2 the C# compiler infers the generic type of the base class for the lambda expression in the 'PropertyInt' property setter, but for Class3 the compiler infers the base class, not just the generic type of the base class. Why is this? What is the criteria for the inferred type in the code sample. Thanks.


First off, the TObject generic parameter is defined in Class1. That TObject is used in Class1 as the type argument in MethodA.

In Class2, the TObject passed to the base (Class1) is a Class2, and so the lambda can infer the local property _propertyInt.

In Class3, the TObject passed to the base is a Class2, not a Class3. Therefore, the lambda's argument is inferred, but it is inferred as a Class2, not a Class3.

The fact that Class2 also has a type parameter named TObject is entirely coincidental- I think you are expecting that anything passed to that TObject will be transitively passed on to Class1, which it is not.

If you defined Class3 as follows, it would work:

public class Class3 : Class1<Class3> { ... }

Given the comment, then might I offer this extension method based solution, (assuming that the type parameters were only for the purpose of making this work):

public class Class1
{
}

public static class StaticClass1
{
    public static void MethodA<TZen, TType>(this TZen zen, Expression<Func<TZen, TType>> property, ref TType store, TType value) where TZen : Class1
    {
        // Do whatever here...
    }
}

public class Class2 : Class1
{
    private int _propertyInt;
    public int PropertyInt
    {
        get { return _propertyInt; }
        set { this.MethodA(c2 => c2.PropertyInt, ref _propertyInt, value); }
    }
}

public class Class3 : Class2
{
    private float _propertyFloat;
    public float PropertyFloat
    {
        get { return _propertyFloat; }
        set { this.MethodA(c3 => c3.PropertyFloat, ref _propertyFloat, value); }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜