G
Guest
Hi All,
I am testing some stuff out at the moment with the runtime generation of
classes relating to schema-validated XML elements. I was hoping to get to the
point that I could include the use of the DefaultValueAttribute on any
property that requires it. The PropertyGrid was then to be used to display
the various properties for editing but, this DefaultValueAttribute doesn't
seem to stick enough for the PropertyGrid to correctly bold/unbold values
entered.
EXAMPLE:
private static void _addAttribute(TypeBuilder tb, string PropertyName)
{
FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(PropertyName,
PropertyAttributes.HasDefault, typeof(string), new Type[] { typeof(string) });
Type t = typeof(System.ComponentModel.CategoryAttribute);
ConstructorInfo ci = t.GetConstructor(new Type[1]{typeof(string)});
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new
string[1]{PropertyName});
pb.SetCustomAttribute(cab);
if (PropertyName == "CustomerName")
{
t = typeof(System.ComponentModel.DefaultValueAttribute);
ci = t.GetConstructor(new Type[1]{typeof(string)});
cab = new CustomAttributeBuilder(ci, new string[1]{"Robert Magnusson"});
pb.SetCustomAttribute(cab);
}
// First, we'll define the behavior of the "get" property for CustomerName
as a method.
MethodBuilder mbGet = tb.DefineMethod("Get" + PropertyName,
MethodAttributes.Public, typeof(string), new Type[] { });
ILGenerator custNameGetIL = mbGet.GetILGenerator();
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, fb);
custNameGetIL.Emit(OpCodes.Ret);
// Now, we'll define the behavior of the "set" property for CustomerName.
MethodBuilder mbSet = tb.DefineMethod("Set" + PropertyName,
MethodAttributes.Public, null, new Type[] { typeof(string) });
ILGenerator custNameSetIL = mbSet.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, fb);
custNameSetIL.Emit(OpCodes.Ret);
// Last, we must map the two methods created above to our PropertyBuilder
to
// their corresponding behaviors, "get" and "set" respectively.
pb.SetGetMethod(mbGet);
pb.SetSetMethod(mbSet);
}
I am testing some stuff out at the moment with the runtime generation of
classes relating to schema-validated XML elements. I was hoping to get to the
point that I could include the use of the DefaultValueAttribute on any
property that requires it. The PropertyGrid was then to be used to display
the various properties for editing but, this DefaultValueAttribute doesn't
seem to stick enough for the PropertyGrid to correctly bold/unbold values
entered.
EXAMPLE:
private static void _addAttribute(TypeBuilder tb, string PropertyName)
{
FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(PropertyName,
PropertyAttributes.HasDefault, typeof(string), new Type[] { typeof(string) });
Type t = typeof(System.ComponentModel.CategoryAttribute);
ConstructorInfo ci = t.GetConstructor(new Type[1]{typeof(string)});
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new
string[1]{PropertyName});
pb.SetCustomAttribute(cab);
if (PropertyName == "CustomerName")
{
t = typeof(System.ComponentModel.DefaultValueAttribute);
ci = t.GetConstructor(new Type[1]{typeof(string)});
cab = new CustomAttributeBuilder(ci, new string[1]{"Robert Magnusson"});
pb.SetCustomAttribute(cab);
}
// First, we'll define the behavior of the "get" property for CustomerName
as a method.
MethodBuilder mbGet = tb.DefineMethod("Get" + PropertyName,
MethodAttributes.Public, typeof(string), new Type[] { });
ILGenerator custNameGetIL = mbGet.GetILGenerator();
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, fb);
custNameGetIL.Emit(OpCodes.Ret);
// Now, we'll define the behavior of the "set" property for CustomerName.
MethodBuilder mbSet = tb.DefineMethod("Set" + PropertyName,
MethodAttributes.Public, null, new Type[] { typeof(string) });
ILGenerator custNameSetIL = mbSet.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, fb);
custNameSetIL.Emit(OpCodes.Ret);
// Last, we must map the two methods created above to our PropertyBuilder
to
// their corresponding behaviors, "get" and "set" respectively.
pb.SetGetMethod(mbGet);
pb.SetSetMethod(mbSet);
}