我正在尝试创建一个委托来设置泛型的属性值,但出现错误:Error binding to target method
当我尝试执行以下代码时:
Action<T, object> setValue = (Action<T, object>) Delegate.CreateDelegate(
typeof(Action<T, object>), null, property.GetSetMethod());
这可能吗?
是的,有可能,您只是试图创建错误类型的委托。属性的 set 方法仅接受一个参数,即您要设置的值。此外,由于它是一个实例方法,因此您必须在 CreateDelegate 调用中传递您希望其绑定到的目标对象。
Example:
var setValue = (Action<T>)Delegate.CreateDelegate( typeof( Action<T> ), target, property.GetSetMethod() );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)