naw*_*fal 1 c# generics typeconverter expression-trees
我有这个静态功能
public static object Create(Type t)
{
//unimportant
}
Run Code Online (Sandbox Code Playgroud)
我无法控制上面的上述功能,所以我无法改变它.问题是它不是通用的,所以我必须将返回的对象转换为某种类型.这个类型是由我调用该Create方法的另一个泛型类的约束提供的.
这是我到达的地方:
public static class Creator<T>
{
public static void Create()
{
var m = typeof(SomeClass).GetMethod("Create");
var p = Expression.Parameter(typeof(Type));
var e = Expression.Call(m, p);
//at this stage I want to create delegate for calling the 'Create' method,
//then pass typeof(T) as parameter, get returned object,
//and finally cast it to 'T'.
//for eg, I can do it like this:
var f = Expression.Lambda<Func<Type, object>>(e, p).Compile();
Func<T> mainThing = () => (T)f(typeof(T));
//is there a way I can achieve in one step?
}
}
Run Code Online (Sandbox Code Playgroud)
在我的上述方法中,我不是在编写最终代表,而是先前一步.如何在编译之前合并演员并Func<T>回来?
你似乎在跳过很多不必要的箍.我不明白你为什么要通过表达式树来做这件事.为什么不呢:
public static class Creator<T>
{
public static void Create()
{
Func<T> mainThing = () => (T)SomeClass.Create(typeof(T));
}
}
Run Code Online (Sandbox Code Playgroud)
???
创建方法调用的表达式树的目的是什么,只是将其转换为一个委托,然后调用一个方法调用?为什么不直接打电话SomeClass.Create?
这里有什么我想念的吗?
要回答您的实际问题:
如何在编译之前合并演员?
使用Expression.Convert()创建表示转换的表达式目录树节点.