我有以下代码:
using System; using System.Linq; using System.Linq.Expressions; public class Program { public static void Main() { Descendant d = new Descendant(); d.TestMethod(); } } public class Base { protected void FigureItOut(Expression > expr) { } } public class Descendant : Base { public void TestMethod() { FigureItOut(c => c.Name); } public String Name { get; set; } }
我收到此编译器错误消息:
The type arguments for method 'Base.FigureItOut(System.Linq.Expressions.Expression >)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
如果我将对ImageItOut的调用更改为:
FigureItOut((Descendant c) => c.Name);
然后它工作.有没有办法通过更改基类来获得第一个编译示例?
我知道如果我将整个Base类设为泛型,就像这样:
public class Base{ protected void FigureItOut (Expression > expr) { } } public class Descendant : Base { public void TestMethod() { FigureItOut(c => c.Name); } public String Name { get; set; } }
然后它工作,但我宁愿不这样做,任何其他可以使用的黑客,也许在方法级别(即以某种方式改变FigureItOut).
调用actual(protected internal
)实现的扩展方法怎么样?唯一的缺点是你必须添加this.
.
这是有效的,因为source
参数(via this
)推断出一种类型TClass
.
public class Base { protected internal void FigureItOut(Expression > expr) { Debug.WriteLine("Got to actual method"); } } public static class BaseExt { public static void FigureItOut (this TClass source, Expression > expr) where TClass : Base { // call the actual method Debug.WriteLine("Got to extension method"); source.FigureItOut(expr); } } public class Descendant : Base { public void TestMethod() { this.FigureItOut(c => c.Name); } public String Name { get; set; } }
作为替代方案(如果internal
是痛苦的话),请考虑将其设置为静态,并使用主要用于类型推断的实例参数:
protected static void FigureItOut(TClass source, Expression > expr) { } public void TestMethod() { FigureItOut(this, c => c.Name); }