【发布时间】:2011-08-30 07:17:33
【问题描述】:
在 .NET 2.0(使用 C# 3.0)中,当我在编译时不知道其类型时,如何为通过反射获得的属性访问器创建委托?
例如如果我有int 类型的属性,我可以这样做:
Func<int> getter = (Func<int>)Delegate.CreateDelegate(
typeof(Func<int>),
this, property.GetGetMethod(true));
Action<int> setter = (Action<int>)Delegate.CreateDelegate(
typeof(Action<int>),
this, property.GetSetMethod(true));
但如果我在编译时不知道该属性是什么类型,我不知道该怎么做。
【问题讨论】:
-
不确定,但以下问题看起来相关:stackoverflow.com/questions/773099/…
-
Func<T>is .NET 3.5.... 这是自定义的Func<T>? -
@Marc Gravell - Whoops 实际上是在一个针对错误平台的临时项目中玩耍,但它确实适用于 .NET 2.0 中的自定义 Func
。
标签: c# reflection delegates