Run-time type creation and field default values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!
I'm writing some code that will enable me to create new types using xml.
Now, the only problem I have is that the default value I've set using the
FieldBuilder.SetConstant method doesn't show up when I pass an instance of
the new typ to a property grid. Any ideas why?

Best regards
Michael
 
Here's some code

/Michael

private FieldBuilder CreateIntField(TypeBuilder typeBuilder,
string fieldName, string defaultValue)
{
int intDefaultValue = Convert.ToInt32(defaultValue);
return CreateFieldFromType(typeBuilder, fieldName, typeof(int),
intDefaultValue);
}


private FieldBuilder CreateFieldFromType(TypeBuilder typeBuilder,
string fieldName,
Type type,
object defaultValue)
{
FieldBuilder fieldBuilder = typeBuilder.DefineField(fieldName, type,
System.Reflection.FieldAttributes.Private);
fieldBuilder.SetConstant(defaultValue);
return fieldBuilder;
}
 

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

Back
Top