开发者

C# run time class creation

开发者 https://www.devze.com 2023-02-08 22:32 出处:网络
I\'m new to C# and I heard that is possible to create classes at run time. Is this true? If开发者_JAVA百科 it\'s true can you give me a simple example on how to do it? I want to create a class from a

I'm new to C# and I heard that is possible to create classes at run time. Is this true? If开发者_JAVA百科 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!


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消