Usman
Usman

Reputation: 2890

Generating Runtime code(structs,classes) in C#

I need to generate structs and classes at run time in C# (might be in CLI) managed environment. Suppose I have all data and data types associated with those data memebers of some class/struct. Now I need to generate structs/classes at runtime, and after that I need to map data inside those runtime created struct fields.

I have read data from an XML file (i.e. BSTR field1, BSTR field2, BSTR field3, BSTR field4, BSTR field5, long field6).

Now I need to create a struct at runtime which has sizeof the sum of all fields (which I listed above) and then I can easily access those fields one by one, so that whole completed struct is formed.

How can I solve this problem?

Regards,

Usman

Upvotes: 2

Views: 2152

Answers (3)

Incognito
Incognito

Reputation: 16597

You need to check CodeDom and Reflection.Emit.

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283793

System.Reflection.Emit has all the tools you need to make that happen. Start with TypeBuilder.

Upvotes: 0

Manfred
Manfred

Reputation: 5666

Dynamically generated typed object may help in your scenario. Vladimir Bodurov has written about it including source code (which I would post here if it was smaller).

Upvotes: 0

Related Questions