日期:2014-05-17  浏览次数:21027 次

反射创建属性 修改属性类型
  private string name;
            [EditorAttribute(typeof(myeditor), typeof(System.Drawing.Design.UITypeEditor))]
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
类似这种 动态创建 属性 怎么给属性修改
 [EditorAttribute(typeof(myeditor), typeof(System.Drawing.Design.UITypeEditor))]
这个属性是一个类  修改属性类型 让属性加载到propertygrid里面显示 点击按钮能弹出框
创建动态的属性没问题  请问怎么修改属性的EditorAttribute   
反射创建属性?修改属性类型

------解决方案--------------------
创建的时候设置
你怎么创建的?
------解决方案--------------------
用MethodBuilder.SetCustomAttribute方法啊
------解决方案--------------------

var constructor = typeof(EditorAttribute).GetConstructor(new Type[] {typeof(Type),typeof(Type)});
var customAttribute = new CustomAttributeBuilder(constructor,new object[] {typeof(myeditor), typeof(UITypeEditor)});
propertyBuilder.SetCustomAttribute(customAttribute);

------解决方案--------------------
建议你看Stephen Toub在MSDN杂志上的ICustomTypeDescriptor, Part 1
http://msdn.microsoft.com/en-us/magazine/cc163816.aspx

很多的控件(PropertyGrid, DataGridView)都认可CustomTypeDescriptor。
一般没有必要用动态类型编译。