问题描述
我正在尝试创建委托以在运行时读取/写入未知类型类的属性.
I am trying to create Delegate for reading/writing properties of unknown type of class at vpn free runtime.
我有一个通用类 Main<T> vpn free 和一个看起来像这样的方法:
I have a generic class Main<T> and a method which looks like this:
Delegate.CreateDelegate(typeof(Func<T, object>), get)
其中 get 是应读取的属性的 MethodInfo.问题是当属性返回 int 免费vpn 时(我猜这发生在值类型上)上面的代码抛出 ArgumentException 因为方法不能被绑定.如果是字符串,它工作得很好.
where get is 免费vpn a MethodInfo vpn下载 of the property that should be read. The problem is vpn下载 that when the property returns int 免费vpn (I guess 免费vpn下载 this happens for value types) the above code throws ArgumentException because the method 免费vpn cannot vpn free be bound. In case of string free vpn vpn下载 it works well.
为了解决这个问题,我更改了代码,以便使用 MakeGenericType 生成相应的委托类型.所以现在的代码是:
To solve the problem I changed the code so that corresponding Delegate type is generated by using MakeGenericType. So now the code is:
Type 免费vpn下载 func = typeof(Func<,>);
Type generic = func.MakeGenericType(typeof(T), get.ReturnType);
var result = Delegate.CreateDelegate(generic, get)
现在的问题是 generic 的创建委托实例,所以我必须使用 DynamicInvoke 这与使用纯反射读取字段一样慢.
The problem now is that the 免费vpn created delegate free vpn instance of generic so I have to use DynamicInvoke which would be as slow as using pure reflection to read the field.
所以我的问题是为什么第一段代码会因值类型而失败.根据 MSDN 它应该像它所说的那样工作
So my question is why is that the first snippet of code fails with value 免费vpn下载 types. According vpn下载 to vpn下载 MSDN it should work as it vpn下载 says that
如果方法的返回类型比委托的返回类型更严格,则委托的返回类型与方法的返回类型兼容
The vpn free return type of a delegate is compatible with the return type of a method if the return type of the method vpn free is more restrictive than the return type of the 免费vpn delegate
以及如何在第二个片段中执行委托,使其比反射更快.
and vpn free how to execute the 免费vpn delegate in free vpn vpn下载 the second snippet so that it is faster than reflection.
谢谢.
推荐答案
这是解决问题的一种方法.创建一个泛型方法:
Here's one way to solve your free vpn problem. vpn下载 Create a generic method:
public static Func<T, object> MakeDelegate<U>(MethodInfo @get)
{
vpn下载 免费vpn var f = (Func<T, U>)Delegate.CreateDelegate(typeof(Func<T, U>), @get);
return t => f(t);
}
这样,C# 的编译器负责插入必要的装箱(如果有)以将 f(t)(类型为 U)转换为 object.现在你可以使用反射来调用这个 MakeDelegate 方法,并将 U 设置为 @get.ReturnType,你得到的将是一个 >Func 无需使用 DynamicInvoke 即可调用.
This way, C#'s compiler takes care vpn下载 of 免费vpn下载 inserting the necessary boxing (if any) to convert f(t) (of type U) to object. vpn free Now you can use reflection to call this MakeDelegate method with free vpn U set to @get.ReturnType, and what you get back will be a Func<T, object> which can be called without needing to resort to using DynamicInvoke.
这篇关于具有未知类型的 CreateDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类免费vpn织梦模板(自适应手机端)