通用Property.GetSetMethod代理 [英] Delegate for Generic Property.GetSetMethod

查看:229
本文介绍了通用Property.GetSetMethod代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个委托来设置一个通用的属性值,但是我收到一个错误:当我尝试执行目标方法以下代码:

I am trying to create a delegate for setting a property value of a generic, but I am getting an error: Error binding to target method when I try to execute the following code:

Action<T, object> setValue = (Action<T, object>) Delegate.CreateDelegate(
    typeof(Action<T, object>), null, property.GetSetMethod());

这是否可能?

推荐答案

是的,这是可能的,你只是想创建一个错误类型的委托。属性的set方法只需要一个参数,你将要设置的值。另外,由于它是一个实例方法,您必须在CreateDelegate调用中传递要绑定到的目标对象。

Yes it is possible, you're just trying to create a delegate of the wrong type. The set method of a property only takes one argument, the value you're going to set. Also since its an instance method, you must pass the target object you want it to be bound to in the CreateDelegate call.

示例:

  var setValue = (Action<T>)Delegate.CreateDelegate( typeof( Action<T> ), target, property.GetSetMethod() );

这篇关于通用Property.GetSetMethod代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆