2

I'm new to C# and I heard that is possible to create classes at run time. Is this true? If it's true can you give me a simple example on how to do it? I want to create a class from a configuration file at run time to use it as SelectedObject in PropertyGrid control.

Thanks for help!

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

4 Answers4

3

You can, with TypeBuilder - but this is not trivial stuff; you are essentially writing low-level things like IL at runtime. I do this occasionally, but I'm a bit crazy. The two may be related.

For what you want (PropertyGrid) a simpler option is to provide runtime properties via PropertyDescriptor; I have several such "property bag" examples here on stackoverflow. Here's one such example: .Net Property Grid. Is there a way to let the Grid manipulate object in different way

But even that is work compared to the simplest option: use a DataTable; add the columns you want and a single row, and pass the row's twin from the table's DefaultView to the PropertyGrid.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

What you actually want to do is display arbitrary data in a PropertyGrid. You don't need to create a new class for this. You can just use a custom type descriptor.

Check this out.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
0

You can do this using the TypeBuilder class. The linked MSDN documentation has an example.

0

I think what you're after is dynamic source code generation which allows you to create classes etc at runtime. Link includes samples.

escouser
  • 1,913
  • 19
  • 15