我们正在使用 DynamicObject
创建动态属性,但随后我们想使用 PropertyGrid
来显示和编辑这些属性。
首先,我找到了this文章和这个one .我尝试使用第二篇文章的代码,但以更通用的方式,基本上是用变量替换所有方法名称常量。但问题是VS2010找不到CSharpGetMemberBinder
类型。
有人知道怎么换吗?或者什么是最好的方法?
最佳答案
您可以简单地转换为 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。如果您使用不同的支持创建了自己的动态类,您可能需要更改它。
关于c# - 如何覆盖 .NET 4 中动态对象的 PropertyDescriptor.GetValue 和 PropertyDescriptor.SetValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4104875/