I created a class a couple of years ago which simplifies the CodeCom handling when you want to dynamically create objects in .NET. I got reminded of it when I read a question at stackoverflow.com.
The class can be used to easily generate .NET (C#) objects by having a class definition in a string variable.
First you have to define an interface (or base class) in your project:
public interface IWorld
{
string Hello(string value);
}
Then you need to add a class to a string variable:
string code = @"namespace MyNamespace
{
class Temp : IWorld
{
public string Hello(string value)
{
return ""World "" + value;
}
}
}";
And let’s generate an object and invoke the method:
Compiler compiler = new Compiler();
compiler.AddType(typeof(string));
compiler.Compile(code);
var obj = compiler.CreateInstance<IWorld>();
string result = obj.Hello("World!");
Note that it was a long time ago that I wrote it. The example might not work 100%. (The Compiler class do work, the example might use it incorrectly).
Source code