4
鉴于以下类:Lambda表达式类型推断在继承链中是不同的。为什么?
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 { MethodA(c3 => c3.PropertyFloat, ref _propertyFloat, value); }
}
}
对于等级2的C#编译器推断通用类型中的“PropertyInt”属性setter lambda表达式基类的,但对于Class3的编译器推断的基类,不只是基类的泛型类型。为什么是这样?代码示例中推断类型的标准是什么。谢谢。
嗯,你完全正确,Chris,TObject类型(在Class1中)永远是Class2,无论链条多深。现在,我在世界上怎么会想念这个?不幸的是,你的解决方案不适用于我的情况,因为它意味着Class3将不再继承Class2的属性。最简单的解决方案是将C3投射为“Class3”。我的问题是关于我认为推论的不一致,但正如你所指出的那样,它并不矛盾。 – 2011-06-02 02:26:15
请参阅上面的编辑,了解可能有所帮助的备用解决方案。 – 2011-06-02 02:41:13