Slave Hack Companion goes Open Source

Hey everyone,

As promised, i’ve released Slave Hack Companion as open source.
Please feel free to send any questions or bug reports, and enhance it as you like.

If you do make changes, consider sending them to me so I can add them to the distribution.



Instantiating Classes through reflection using C# – Dynamic Object Creation

As I mentioned earlier, i’ve been working on a personal project in the few short hours I can steal each week.  Whilst I’m not ready to talk about the project itself, I did come across a particular situation I thought may be of interest.

The problem I had to solve was creating a class that would return a simple constant value.  It had to implement a certain interface however and, for various reasons, it wasn’t as simple as assigning the value to the class to return.  I needed to hard code the value into the class, but I needed to do it at runtime (so the value can be different for each instantiation.)  Basically, I needed to compile and create an instance of the object at run-time, with dynamically generated source code for the object itself.

Turns out, .Net 2.0 makes that quite easy.  Heres how it’s done:

How you generate your source-code is an issue all in itself, and was fairly simple in this case, but what you need is a string variable that contains the source code for the class you want to compile.  It needs to be complete as the system will compile it to an assembly.  An example is below:

string code =
    “using system;” +
    “namespace ExampleCode.Source {” +
    “public class helloWorld {” +
    “private string displayString; ” +
    “public helloWorld() {“
    “displayString = “Hello World!”; }” +
    “public string DisplayString() {” +
    “return displayString;}” +

What you need then is the fully qualified type name of the class you want to instantiate.  In this case it’s going to be:

string typeName = “ExampleCode.Source.helloWorld”;

Compiling is a simple matter of calling the CompileAssemblyFromSource method of a CSharpCodeProvider.  A similar provider exists for VB.Net

CSharpCodeProvider compiler = new CSharpCodeProvider();

You need to pass in parameters however that visual studio normally takes care of for you, in particular the libraries to link to.

CompilerParameters compilerParams = new CompilerParameters(new string[] { “System.dll” });

The constructor parameters listed here is simply and array containing the name of every type you want to link to the assembly you are about to compile. System dlls and those of the current project can normally be added with a name only, for others you may need a full pathname.
CompilerParameters includes a lot of other functionality for controlling how the compilation project works, you may wish to check it out if you’re compiling something more complicated.

CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, new string[] { code });

All you need is a set of parameters and the code to compile and you’ve got an assembly.  The results returned contains a lot of information about how the compilation went and may contain a list of errors if the compile wasn’t successful.  You may have to output them and check while you’re first setting up to catch the little typos and logic errors that creep in to your dynamic code.  What it will also contain is the in-memory representation of the assembly itself. 

Getting your object instantiation now is easy:

object hw = results.CompiledAssembly.CreateInstance(typeName);

If everything has worked you’ve now got an instance of your dynamic class, just like all your design time compiled classes.  It’s an object only as your compiler knows nothing about it at runtime. You can get around this by having your class inherity from a base, or implement a known interface, and cast the object to one of these.

Hope this is helpful!

SlaveHack Companion Open Source News

Hey everyone and thanks for your patience,

I havn’t been able to post much the last couple of weeks as i’ve just recently started an excellent new job that keeps me pretty busy.  I’ve also been buckling into my study pretty hard as well, with both full time work and study I don’t have a lot of free time.  On top of that, i’ve recently started a new personal project that i’ve been thinking about for years.  I wont talk too much about it yet as i’m not very far along, but i’m pretty excited about it.

For those of you that have been waiting to see if I open source SlaveHack Companion, i’m happy to say it’s not far from happening. I’ve found the source code and although it’s hardly the greatest thing i’ve ever written, it all compiles and works fine.  I’ve submitted a registration request to Sourceforge and if it’s approved, i’ll set up the project space right away and make the source code available for download.

I’m not familiar with all sourceforge offers, but hopefully there will also be a bug tracking feature.  I’m not actively developing the companion at the moment (There is so much i’d like to change that it would be too great a time investment), but I will be fixing any bugs that are found and, hopefully, if anyone decides to make their own improvements i’ll be happy to consider merging them into the main source.

It’s been an exciting time for me lately, but i’m hoping to find more time to post soon. Stay tuned!