O2 Script: Dynamic creation of .Net Assemblies, Types, Methods, Properties and Fields
While creating an WSDL-based Webservice’s Invocation script, I needed the ability to create dynamic classes (for the creation and serialization of Soap Requests). So based on this Create your own new Type and use it on run-time (C#) example, I added a new set of Extension methods to O2 that expose .NET’s ability to dynamically create assemblies, types, methods, properties and fields (see new O2 script DynamicTypes.cs)
Here is how a new Class (with properties) can be created and populated:
var assemblyBuilder = "assemblyName".assemblyBuilder();
var dynamicModule = assemblyBuilder.dynamicModule("dynModule");
var dynamicType = dynamicModule.dynamicType("dynType");
dynamicType.add_Property<string>("test_String");
dynamicType.add_Property<int>("test_Int");
dynamicType.add_Property<List<int>>("test_Int_List");
var _object = dynamicType.CreateType().ctor();
_object.property("test_String", "test value");
_object.property("test_Int", 111);
_object.property("test_Int_List", new List<int>().add(12).add(13));
return _object.serialize(false);
the last line returns the created object’s serialized string, which looks like this
<?xml version="1.0"?> <dynType> <test_String>test value</test_String> <test_Int>111</test_Int> <test_Int_List> <int>12</int> <int>13</int> </test_Int_List> </dynType>
See Create your own new Type and use it on run-time for the amount of code that is normaly needed to achieve the same result


[...] O2 Script: Dynamic creation of .Net Assemblies, Types, Methods, Properties and Fields [...]