<?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>Kerry D. Wong &#187; Dynamic Assembly</title>
	<atom:link href="http://www.kerrywong.com/tag/dynamic-assembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrywong.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 00:51:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Yet Another Guide to Dynamic Assembly loading and Execution Using Reflection</title>
		<link>http://www.kerrywong.com/2007/07/06/yet-another-guide-to-dynamic-assembly-loading-and-execution-using-reflection/</link>
		<comments>http://www.kerrywong.com/2007/07/06/yet-another-guide-to-dynamic-assembly-loading-and-execution-using-reflection/#comments</comments>
		<pubDate>Sat, 07 Jul 2007 00:49:18 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Dynamic Assembly]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://dimension/2007/07/06/yet-another-guide-to-dynamic-assembly-loading-and-execution-using-reflection/</guid>
		<description><![CDATA[Download ReflectionTest.zip There are already many good guides out there illustrating how to construct a weakly coupled system that dynamically loads in modules as needed. In .Net, this can be accomplished using reflection. However, some people are confused about Reflection versus Serialization, thinking that a class must be serializable before it can be dynamically loaded. [...]]]></description>
			<content:encoded><![CDATA[<p class="itemBody">Download <a href="/blog/wp-content/uploads/2007/09/reflectiontest.zip">ReflectionTest.zip</a></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">There are already many good guides out there illustrating how to construct a weakly coupled system that dynamically loads in modules as needed. In .Net, this can be accomplished using reflection.</font></span><span id="more-177"></span><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000"> <o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">However, some people are confused about Reflection versus Serialization, thinking that a class must be serializable before it can be dynamically loaded. This is not the case however. Reflection is typically used to reflect upon the object&rsquo;s type, member information and dynamically invoke object&rsquo;s methods whereas serialization is typically used to transfer objects&rsquo; states across wires.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">Here I will give a very simple example to show how reflection can be used to create pluggable application architecture.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">First, let&rsquo;s create an interface that defines the common functionalities of our plug-ins. It is always desirable to define a common interface for all the plug-in modules as the main application that loads and executes these modules need not to have any information about the plugged in objects.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">    </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> </font><strong><span style="color: blue;">interface</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong><span style="color: rgb(43, 145, 175);"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">void</span></strong><font color="#000000"> Test();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">Next, we create a simple pluggable module that is based on this interface.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">    </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> </font><strong><span style="color: blue;">class</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">PlugIn</span></strong><font color="#000000"> : PlugInInterface</font><span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong><span style="color: rgb(43, 145, 175);"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">private</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong><font color="#000000"> _ht;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong><font color="#000000"> Hash<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: blue;">get</span></strong><font color="#000000"> {</font><strong><span style="color: blue;">return</span></strong><font color="#000000"> _ht;}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: blue;">set</span></strong><font color="#000000"> {_ht </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: blue;">value</span></strong><font color="#000000">;}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> PlugIn()<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>            </span>_ht </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: blue;">new</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong><font color="#000000">();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>            </span>_ht[</font><span style="color: rgb(163, 21, 21);">&quot;Key&quot;</span><font color="#000000">] </font><span style="color: navy;">=</span><font color="#000000"> </font><span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span><font color="#000000">;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> </font><strong><span style="color: blue;">void</span></strong><font color="#000000"> Test()<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: rgb(43, 145, 175);">Console</span></strong><span style="color: navy;">.</span><font color="#000000">WriteLine(_ht[</font><span style="color: rgb(163, 21, 21);">&quot;Key&quot;</span><font color="#000000">]</font><span style="color: navy;">.</span><font color="#000000">ToString());<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">In the above example, the module does nothing but adds an entry to a hashtable. The point I want to make here is that since we will be using reflection mechanism to invoke this class, no serialization is required. In fact an hashtable cannot be simply serialized by the Serializable() attribute as XmlSerializer does not serialize objects that implement IDictionary by default (see <a href="http://msdn.microsoft.com/msdnmag/issues/03/06/XMLFiles/">this article</a> on MSDN on how to get around this). <o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">Here is the main class that loads the plugin assembly using reflection:<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">    </font></span><strong><span style="color: blue;">class</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Program</span></strong><span style="color: rgb(43, 145, 175);"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">public</span></strong><font color="#000000"> </font><strong><span style="color: blue;">void</span></strong><font color="#000000"> Test()<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: blue;">string</span></strong><font color="#000000"> path </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Path</span></strong><span style="color: navy;">.</span><font color="#000000">Combine(</font><strong><span style="color: rgb(43, 145, 175);">Environment</span></strong><span style="color: navy;">.</span><font color="#000000">CurrentDirectory, </font><span style="color: rgb(163, 21, 21);">&quot;..\\..\\..\\PlugInLib\\bin\\debug&quot;</span><font color="#000000">);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: blue;">string</span></strong><font color="#000000"> fileName </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Path</span></strong><span style="color: navy;">.</span><font color="#000000">Combine(path, </font><span style="color: rgb(163, 21, 21);">&quot;PlugInLib.dll&quot;</span><font color="#000000">);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: rgb(43, 145, 175);">Assembly</span></strong><font color="#000000"> asm </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Assembly</span></strong><span style="color: navy;">.</span><font color="#000000">LoadFrom(fileName);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: rgb(43, 145, 175);">Type</span></strong><font color="#000000"> type </font><span style="color: navy;">=</span><font color="#000000"> asm</font><span style="color: navy;">.</span><font color="#000000">GetType(</font><span style="color: rgb(163, 21, 21);">&quot;PlugInLib.PlugIn&quot;</span><font color="#000000">);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>            </span>PlugInInterface</font><span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong><font color="#000000"> cls </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Activator</span></strong><span style="color: navy;">.</span><font color="#000000">CreateInstance(type) </font><strong><span style="color: blue;">as</span></strong><font color="#000000"> PlugInInterface</font><span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong><font color="#000000">;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>            </span>cls</font><span style="color: navy;">.</span><font color="#000000">Test();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">        </font></span><strong><span style="color: blue;">static</span></strong><font color="#000000"> </font><strong><span style="color: blue;">void</span></strong><font color="#000000"> <st1:place w:st="on">Main</st1:place>(</font><strong><span style="color: blue;">string</span></strong><font color="#000000">[] args)<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>{<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span><font color="#000000">            </font></span><strong><span style="color: rgb(43, 145, 175);">Program</span></strong><font color="#000000"> p </font><span style="color: navy;">=</span><font color="#000000"> </font><strong><span style="color: blue;">new</span></strong><font color="#000000"> </font><strong><span style="color: rgb(43, 145, 175);">Program</span></strong><font color="#000000">();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>     </span><span>       </span>p</font><span style="color: navy;">.</span><font color="#000000">Test();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>        </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font color="#000000"><span>    </span>}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">Since the plugin implements PlugInInterface, it is desirable to put the interface in its own class library so that we only need to make a reference to the interface class library not the actual plug-in itself.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font color="#000000">The full example code can be downloaded <a href="/blog/wp-content/uploads/2007/09/reflectiontest.zip">here</a> (in C#, Framework 2.0).</font></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/07/06/yet-another-guide-to-dynamic-assembly-loading-and-execution-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
