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 反射创建属性 修改属性类型
[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 反射创建属性 修改属性类型
for (int i = 0; i < objXMLNode.ChildNodes.Count; i++)
{
strPropertyName = objXMLNode.ChildNodes[i].Name;
// Add the class variable, such as "m_strIPAddress"
fieldBuilder = typeBuilder.DefineField("_" + strPropertyName, typeof(string), FieldAttributes.Public);
propertyBuilder = typeBuilder.DefineProperty(strPropertyName, System.Reflection.PropertyAttributes.HasDefault, typeof(string), null);
getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
methodBuilder = typeBuilder.DefineMethod("get_" + strPropertyName, getSetAttr, typeof(string), Type.EmptyTypes);
methodBuilder.SetReturnType(typeof(string));
ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
ilGenerator.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(methodBuilder);
methodBuilder = typeBuilder.DefineMethod("set_" + strPropertyName,
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName,
typeof(void), methodArgs);
ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
ilGenerator.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(methodBuilder);
}
public class myeditor : UITypeEditor
{
public myeditor()
{ }
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
string str = null;
if (edSvc != null)
{
//test f = new test();
// your setting here
StatisticalCategoryForm f = new StatisticalCategoryForm();
DialogResult dr = edSvc.ShowDialog(f);
if (dr == DialogResult.OK)
{
foreach (KeyValuePair<string,string> kvp in Customer.Diction)
{
if (kvp.Value != "")
str += kvp.Key+ kvp.Value;
else
str += kvp.Key + " ";
}
}
}
value = str;
return value;
}
public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return false;
}
}
属性的类型就是这个类 不知道该怎么写
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);
http://msdn.microsoft.com/en-us/magazine/cc163816.aspx很多的控件(PropertyGrid, DataGridView)都认可CustomTypeDescriptor。
一般没有必要用动态类型编译。