c# - 如何覆盖 .NET 4 中动态对象的 PropertyDescriptor.GetValue 和 PropertyDescriptor.SetValue

标签 c# dynamic binding c#-4.0

我们正在使用 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/

相关文章:

Python动态多重继承

wpf - 触发命令时强制绑定(bind)更新

c# - 绑定(bind)到后面代码中的相关源

c# - 初始化引用类型对象数组的简洁方式

.NET 4.0 中的 C# Task.Run 等价物

c# - 访问路径 C :\is denied

c# - 字符串正在删除文本中的\t,而我想要反斜杠后面的所有内容

iphone - 在 Objective-C 中动态加载类?

jquery - 使用 JQuery 处理动态字段时 Grails 表单提交失败

c# - 为什么我需要覆盖托管包装委托(delegate)对象中的可选 ObjC 方法?