DefaultAttribute not working in PropertyGrid on runtime generated

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);
}
 
C

Claes Bergefall

You say that it doesn't correctly bold/unbold values.
Which is it (alwyas bold or always normal)?
Is Reset enabled or disabled?

What does your generated code look like?

Have you intialized the property to its default value?
Just providing the the attribute isn't enough

/claes
 
G

Guest

Claes,
What does your generated code look like?
How can I see it?
Which is it (alwyas bold or always normal)?
It always stays bold. I have added the SetConstant call as below but I
don't see any difference to what's happening.

FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
(PropertyName == "CustomerName" ? FieldAttributes.Private |
FieldAttributes.HasDefault : FieldAttributes.Private));

fb.SetConstant(@"Robert Magnusson");

Thanks for your help...

Claes Bergefall said:
You say that it doesn't correctly bold/unbold values.
Which is it (alwyas bold or always normal)?
Is Reset enabled or disabled?

What does your generated code look like?

Have you intialized the property to its default value?
Just providing the the attribute isn't enough

/claes

Robert Magnusson said:
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);
}
 
C

Claes Bergefall

Robert Magnusson said:
Claes,

How can I see it?

I don't know. I was hoping you could tell me :)
Isn't there some kind of persisted ouput you can look at
(perhaps some dll that you can decompile)?
Perhaps you could save it to a file somehow.

It always stays bold. I have added the SetConstant call as below but I
don't see any difference to what's happening.

FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
(PropertyName == "CustomerName" ? FieldAttributes.Private |
FieldAttributes.HasDefault : FieldAttributes.Private));

fb.SetConstant(@"Robert Magnusson");

There is a SetValue method in the FieldBuilder class aswell.
Have you tried that? I don't really know what the difference
is but it's worth trying.

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top