<?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>Florent Clairambault &#187; .Net Add-In</title>
	<atom:link href="http://florent.clairambault.fr/tag/net-add-in/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Thu, 09 Sep 2010 23:45:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Loading plugins assemblies in .Net</title>
		<link>http://florent.clairambault.fr/loading-plugins-assemblies-in-net?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=loading-plugins-assemblies-in-net</link>
		<comments>http://florent.clairambault.fr/loading-plugins-assemblies-in-net#comments</comments>
		<pubDate>Tue, 01 Dec 2009 18:00:37 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net Add-In]]></category>
		<category><![CDATA[.Net plugins]]></category>
		<category><![CDATA[C# .Net]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2267</guid>
		<description><![CDATA[(first post of the year) This might seem like a quite complex thing to do, but it&#8217;s in fact very simple. Thank you .Net for being so well built. Note : With .Net 3.5, there is a much more advanced method called Add-In. But it&#8217;s also much more complex. You should use it on long-term [...]]]></description>
			<content:encoded><![CDATA[<p><em>(first post of the year)</em></p>
<p>This might seem like a quite complex thing to do, but it&#8217;s in fact very simple. Thank you .Net for being so well built.</p>
<p>Note : With .Net 3.5, there is a much more advanced method called <a href="http://msdn.microsoft.com/en-us/library/bb384200.aspx">Add-In</a>. But it&#8217;s also much more complex. You should use it on long-term projects with some evolutions of the plugins API (and no possibility to change the plugins). I&#8217;ve used it for a project and that really made us lose a lot of time.</p>
<p>So here is the code for a simple plugins class loading system :</p>

<div class="wp_codebox"><table><tr id="p22675"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p2267code5"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span> GetPlugins<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> folder <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	var files <span style="color: #008000;">=</span> Directory<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFiles</span><span style="color: #008000;">&#40;</span> folder, <span style="color: #666666;">&quot;*.dll&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var tList <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> file <span style="color: #0600FF; font-weight: bold;">in</span> files <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
			var assembly <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadFile</span><span style="color: #008000;">&#40;</span> file <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> Type type <span style="color: #0600FF; font-weight: bold;">in</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTypes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>type<span style="color: #008000;">.</span><span style="color: #0000FF;">IsClass</span> <span style="color: #008000;">||</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNotPublic</span> <span style="color: #008000;">&#41;</span>
					<span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
				var interfaces <span style="color: #008000;">=</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInterfaces</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>IList<span style="color: #008000;">&#41;</span> interfaces <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
					tList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> type <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception ex <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogException</span><span style="color: #008000;">&#40;</span> ex <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> tList<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> InstanciatePlugins<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> List<span style="color: #008000;">&lt;</span>Type<span style="color: #008000;">&gt;</span> types, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args <span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T<span style="color: #008000;">:</span><span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #008000;">&#123;</span>
	var list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> var t <span style="color: #0600FF; font-weight: bold;">in</span> types <span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">GetInterfaces</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IList <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
			list<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> Activator<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span> t, args <span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> T <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> list<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Our project can be organized like this :<br />
Project.Project : The application that will load plugins<br />
Project.Common.Plugins : The common types used by the core and the plugins<br />
Project.Plugin.Test1 : One stupid test plugin</p>
<p>In Project.Common.Plugins, we will declare an interface :</p>

<div class="wp_codebox"><table><tr id="p22676"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2267code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Project<span style="color: #008000;">.</span><span style="color: #0000FF;">Common</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Plugins</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IPlugin <span style="color: #008000;">&#123;</span>
		<span style="color: #6666cc; font-weight: bold;">String</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">void</span> DoSomeStuff<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In Project.Plugin.Test1, we will declare a class :</p>

<div class="wp_codebox"><table><tr id="p22677"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2267code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> Project<span style="color: #008000;">.</span><span style="color: #0000FF;">Plugin</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Test1</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PluginTest1 <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">String</span> Name <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;PluginTest1&quot;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoSomeStuff<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #008000;">&#41;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;I only count ({0})&quot;</span>, i <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This assembly has to be generated in a &#8220;plugins&#8221; directory.</p>
<p>Then, in your project, you will just have to use the methods given in the beginning and do something like that :</p>

<div class="wp_codebox"><table><tr id="p22678"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2267code8"><pre class="csharp" style="font-family:monospace;">var types <span style="color: #008000;">=</span> GetPlugins<span style="color: #008000;">&lt;</span>IPlugin<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;plugins&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var pluginInstances <span style="color: #008000;">=</span> InstanciatePlugins<span style="color: #008000;">&#40;</span> types, <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Plugins are :&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var pi <span style="color: #0600FF; font-weight: bold;">in</span> pluginInstances <span style="color: #008000;">&#41;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;* {0}&quot;</span>, pi<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you&#8217;re worried that you&#8217;re stuck with these created objects, you should take a look on the AppDomain (I think I will talk about them pretty soon). This allows to load .Net assemblies and types and then unload them when ever you want. But as it can be easily adapted to some existing code, you should start without it and then add it when you feel your application could benefit from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/loading-plugins-assemblies-in-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
