![](/img/trans.png)
[英]PropertyDescriptor.SetValue not working in ModelBinder
[英]How to override PropertyDescriptor.GetValue and PropertyDescriptor.SetValue for dynamic object in .NET 4
您可以简单地转换为IDictionary并设置/检索值,而不是使用该文章的Helper类(已过时)。
public override object GetValue(object component)
{
if (_owner != component) throw new InvalidOperationException("GetValue can only be used with the descriptor's owner.");
//return DynamicHelper.GetValue(component, _propertyName);
return ((IDictionary<String, object>)component)[_propertyName];
}
public override void SetValue(object component, object value)
{
if (_owner != component) throw new InvalidOperationException("SetValue can only be used with the descriptor's owner.");
OnValueChanged(component, EventArgs.Empty);
//DynamicHelper.SetValue(component, _propertyName, value);
((IDictionary<String, object>)component)[_propertyName] = value;
}
编辑:这可能仅适用于ExpandoObjects,这是文章正在使用的..如果您创建自己的动态类具有不同的支持,您可能需要更改它。
您可以使用开源框架Dynamitey,它允许您通过字符串名称调用任何 IDynamicMetaObjectProvider
的动态属性。
public override object GetValue(object component)
{
return Dyanmic.InvokeGet(component,propertyName);
}
public override void SetValue(object component, object value)
{
Dyanmic.InvokeSet(component,propertyName, value);
}
它应该是这样的:
CallSiteContainer.getLengthSite = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None,
"Length",
typeof(Program),
new CSharpArgumentInfo[] {
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
和相关的集合将是这样的:
CallSiteContainer.setLengthSite = CallSite<Func<CallSite, object, object, object>>.Create(Binder.SetMember(CSharpBinderFlags.None,
"Length",
typeof(Program),
new CSharpArgumentInfo[] {
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant | CSharpArgumentInfoFlags.UseCompileTimeType, null)
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.