<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael Clarke Blog &#187; Programming</title>
	<atom:link href="http://www.michael-clarke-blog.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michael-clarke-blog.com</link>
	<description>The Ramblings of a PhD Student</description>
	<lastBuildDate>Fri, 11 Nov 2011 11:32:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>C# &#8211; Dynamic Class Loading</title>
		<link>http://www.michael-clarke-blog.com/2010/08/c-dynamic-class-loading/</link>
		<comments>http://www.michael-clarke-blog.com/2010/08/c-dynamic-class-loading/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 12:52:04 +0000</pubDate>
		<dc:creator>Michael Clarke</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Mono/.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.michael-clarke-blog.com/?p=77</guid>
		<description><![CDATA[I have noticed since moving this blog that a lot of people are trying to access some of my old posts. In particular, people seem very interested in my posts on C# Dynamic Class Loading. As such, I have decided to re-post the old version here for you. Note that this is a copy-and-paste job [...]]]></description>
			<content:encoded><![CDATA[<p>I have noticed since moving this blog that a lot of people are trying to access some of my old posts. In particular, people seem very interested in my posts on C# Dynamic Class Loading. As such, I have decided to re-post the old version here for you. Note that this is a copy-and-paste job from my old blog &#8211; there is nothing new except for a few minor typing corrections that were noticed by Chris, a commentator on my old blog &#8211; thanks! I hope this is useful for those people trying to find it.</p>
<p><em>The other day I wrote a small tutorial on getting dynamic class loading working for Java. In that post I mentioned that it could also be done in C# &#8211; but that it was more difficult, and from my personal experience, there is less information available about it &#8211; so here is how to do it!</em></p>
<p><em>First, as with the Java, we need to create our API. However, unlike the Java there is an additional requirement &#8211; a ‘module manager’ class needs to be within the API. As such you will need to create these two files…</em></p>
<pre>using System;
using System.Reflection;

namespace com.michaelclarkeblog.dynamic.api {

    public static class ModuleManager {

        public static Module getInstance(String fileName) {

            /* Load in the assembly. */
            Assembly moduleAssembly = Assembly.LoadFile(fileName);

            /* Get the types of classes that are in this assembly. */
            Type[] types = moduleAssembly.GetTypes();

            /* Loop through the types in the assembly until we find
             * a class that implements a Module.
             */
            foreach (Type type in types) {
                if (type.GetInterface("Module") != null) {
                    /* Create a new instance of the 'Module'. */
                    return (Module)Activator.CreateInstance(type);
                }
            }

            return null;

        }

    }

}</pre>
<pre>using System;

namespace com.michaelclarkeblog.dynamic.api {

    public interface Module {
        String getText();
    }

}</pre>
<p>﻿<em>Once you’ve got the two files you can compile them into a library….</em></p>
<pre>api $ mcs -t:library Module.cs ModuleManager.cs
api $ ls -a | grep .dll
Module.dll
api $</pre>
<p><em>Now that we’ve made the API the next thing to write is the application that is going to use it for loading modules. This is a very simple application…</em></p>
<pre>using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.application {

    public class Application {

        public static void Main(String[] args) {

            if (args.Length &gt; 0 ) {
                Module module = ModuleManager.getInstance(args[0]);
                Console.WriteLine(module.getText());
            } else {
                Console.WriteLine("Usage: mono Application module_name");
            }
        }

    }

}</pre>
<p><em>Now we can compile our ‘Application’ against the API…</em></p>
<pre>application $ mcs -reference:../api/Module.dll Application.cs
application $ ls -la | grep exe
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:32 Application.exe
application $</pre>
<p><em>And now finally we can write a couple of modules!</em></p>
<pre>using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.modules {
  public class Mike : Module {
    public String getText() {
      return "Hello from Mike's Module!";
    }
  }
}</pre>
<pre>using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.modules {
  public class Another : Module {
    public String getText() {
      return "Hello from Another Module!";
    }
  }
}</pre>
<p><em>Compile the modules…</em></p>
<pre>modules $ mcs -t:library -reference:../api/Module.dll Another.cs
modules $ mcs -t:library -reference:../api/Module.dll Mike.cs
modules $ ls -la | grep dll
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:36 Another.dll
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:36 Mike.dll
modules $</pre>
<p><em>There is just one last thing we need to do before we can run our application. We need to copy the Module.dll API from the api directory into the application directory…</em></p>
<pre>modules $ cd ../application
application $ cp ../api/Module.dll .</pre>
<p><em>And now lets try running our dynamic application…</em></p>
<pre>application $ mono Application.exe ../modules/Mike.dll
Hello from Mike's Module!

application $ mono Application.exe ../modules/Another.dll
Hello from Another Module!

application $ mono Application.exe
Usage: mono Application module_name</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.michael-clarke-blog.com/2010/08/c-dynamic-class-loading/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

