OWASP O2 Platform Blog

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

April 2, 2011 - Posted by | .NET

1 Comment »

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

    Pingback by Script to fetch and present large number of Wordpress.com blog entries « O2Platform.com for Developers | April 16, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: