首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

反照创建属性 修改属性类型

2013-03-25 
反射创建属性 修改属性类型private string name[EditorAttribute(typeof(myeditor), typeof(System.Drawi

反射创建属性 修改属性类型
  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。
一般没有必要用动态类型编译。


热点排行