anc*_*dra 5 .net c# generics delegates properties
我想创建委托来访问不同对象的属性而不事先知道它们.
我有以下定义
public delegate T MyMethod<K, T>(K data);
public static MyMethod<K, T> CreatePropertyGetter<K, T>(PropertyInfo property)
{
MethodInfo mi = property.DeclaringType.GetMethod("get_" + property.Name);
return (MyMethod<K, T>)Delegate.CreateDelegate(typeof(MyMethod<K, T>), mi);
}
Run Code Online (Sandbox Code Playgroud)
其中T可以是decimal,string,datetime或int
我有一些初始化代码将根据我的对象的反射属性创建MyMethod委托,如下所示:
foreach (PropertyInfo property in entityType.GetProperties())
{
switch (property.PropertyType.Name)
{
case "System.Decimal":
return CreatePropertyGetter<T, decimal>(property);
case "System.DateTime":
return CreatePropertyGetter<T, DateTime>(property);
case "System.String":
return CreatePropertyGetter<T, DateTime>(property);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法
编辑:
我关注的是性能,因为这些代表会被频繁调用(滴答场景),因此任何强制转换都会降低它的速度.虽然需要更优雅的解决方案,但性能仍然是我主要关注的问题
我在这里发布了关于Code Review的相同问题,所以考虑到那里的响应,我会将其标记为已解决
这是可以发布在 Code Review 上的内容,实际上,我已经发布了类似的问题。我相信我使用表达式树的方法已经改进了您的方法。
使用示例:
Action<object> compatibleExecute =
DelegateHelper.CreateCompatibleDelegate<Action<object>>( owner, method );
Run Code Online (Sandbox Code Playgroud)
必要时进行转换。这里传递给函数的方法可以有任何类型的参数。
更新:
我还没有对此进行过测试,但对于您的情况,您可以尝试以下操作:
Func<object> getter =
DelegateHelper.CreateCompatibleDelegate<Func<object>>( owner, method );
Run Code Online (Sandbox Code Playgroud)
method必须设置为您检索到的 getter。owner必须设置为您的对象的实例。如果您希望允许将所有者作为参数传递给委托,则必须调整代码。Vladimir Matveev 在对Jon Skeet 文章的评论中给出了一个例子。
static Func<T, object, object> MagicMethod<T>(MethodInfo method)
{
var parameter = method.GetParameters().Single();
var instance = Expression.Parameter(typeof (T), "instance");
var argument = Expression.Parameter(typeof (object), "argument");
var methodCall = Expression.Call(
instance,
method,
Expression.Convert(argument, parameter.ParameterType)
);
return Expression.Lambda<Func<T, object, object>>(
Expression.Convert(methodCall, typeof (object)),
instance, argument
).Compile();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2208 次 |
| 最近记录: |