views:

71

answers:

2

Hi, i have a xml like this

I want to parse the xml, build a dynamic class with spscified properties. i found some pointers to do it with system.reflection.emit namespace, but i do i always have to create an assembly and module in order to define the type? can i just create a type and define the properties?

<Root>
<type>
<name>mytype</name>
 <properties>
  <property>
    <name>property1</name>
    <value>2</value>
    <datatype>int</datatype>
  </property>
  <property>
    <name>property3</name>
    <value>2.5</value>
    <datatype>double</datatype>
  </property>
  <property>
    <name>property4</name>
    <value>hello world</value>
    <datatype>string</datatype>
  </property>
 </properties>
</type>
</Root>
A: 

A type must always exist in an Assembly - so you'll have to create the assembly in memory. You might find it a bit easier to use the CodeDom model to define the types.

Paul Alexander
thanks, thats what i thought..
jyotishka bora
can someone provide an example?
jyotishka bora
A: 

This example shows creating an AssemblyBuilder then a ModuleBuilder then the TypeBuilder. From there you have to define properties. You have to create methods that have return & parameters that match the get and set accessors. The example shows a basic implementation of standard get/set operations that use a field for storage. If you need more then you'll have to learn CIL to boot.

When all done call TypeBuilder.CreateType and possibly AssemblyBuilder.Save if you wish to save it for future use.

As for your apprehension about creating an assembly. It's not that big of a deal to define a dynamic assembly & module. It's only a few lines to do that.

Colin Burnett