<?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; Reflection</title>
	<atom:link href="http://www.kerrywong.com/tag/reflection/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>Event Logging Using Reflection</title>
		<link>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/</link>
		<comments>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/#comments</comments>
		<pubDate>Mon, 26 May 2008 01:47:46 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/</guid>
		<description><![CDATA[When exceptions occur during the execution of&#160;an application, the stack trace contains the detailed location of where the exception is originated.&#160;And for a properly designed application (e.g. all exceptions are properly propagated), this information is adequate. However, some applications need the logging of information not only when exception occurs but along its critical execution path [...]]]></description>
			<content:encoded><![CDATA[<p>When exceptions occur during the execution of&nbsp;an application, the stack trace contains the detailed location of where the exception is originated.<span id="more-290"></span>&nbsp;And for a properly designed application (e.g. all exceptions are properly propagated), this information is adequate. However, some applications need the logging of information not only when exception occurs but along its critical execution path as well. As a result, I see a lot of programs having the location information (for example class names and function names, etc.) hard coded where logging is taken place. This approach, however, tends to introduce inconsistencies over time as the application&nbsp;is modified by many different developers and sometimes the changes to function or class names are not in sync with what is being logged.</p>
<p>This problem can be easily circumvented by using reflections. The following code snippet demonstrates how the current function&#8217;s fully qualified name can be obtained using reflection:</p>
<p><font size="2"></p>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Type</span> t <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: blue;">this</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetType();</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> aName <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> t<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>FullName;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> mName <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: rgb(43, 145, 175);">MethodBase</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetCurrentMethod()<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Name;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Console</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>WriteLine(<span style="font-weight: bold; color: blue;">string</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Format(<span style="font-weight: bold; color: rgb(163, 21, 21);">&quot;{0}.{1}&quot;</span>, aName, mName));</div>
<p><!--EndFragment--></font></p>
<p>And reflection can also be used to obtain the values of member fields (e.g. public and private, etc.):</p>
<p><font size="2"></p>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Type</span> t <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: blue;">this</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetType();</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">FieldInfo</span>[] fields <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> t<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetFields(<span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Instance <span style="font-weight: bold; color: rgb(128, 128, 192);">|</span> <span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>NonPublic <span style="font-weight: bold; color: rgb(128, 128, 192);">|</span> <span style="font-weight: bold; color: olive;">BindingFlags</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Public);</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">foreach</span> (<span style="font-weight: bold; color: rgb(43, 145, 175);">FieldInfo</span> field <span style="font-weight: bold; color: blue;">in</span> fields)</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue;">string</span> fieldValue <span style="font-weight: bold; color: rgb(128, 128, 192);">=</span> <span style="font-weight: bold; color: rgb(43, 145, 175);">String</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Concat(field<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>Name, <span style="font-weight: bold; color: rgb(163, 21, 21);">&quot;: &quot;</span>, field<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>GetValue(<span style="font-weight: bold; color: blue;">this</span>)<span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>ToString());</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: rgb(43, 145, 175);">Console</span><span style="font-weight: bold; color: rgb(128, 128, 192);">.</span>WriteLine(fieldValue);</div>
<div style="margin: 0px; background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: Courier New;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<p><!--EndFragment--></font></p>
<p><!--EndFragment--></p>
<p>This is particularly helpful when logging exceptions, as the member variables&#8217; states would be preserved and it might provide valuable information in determining the causes of the exceptions.</p>
<p>We need to be careful where we use the techniques mentioned above however, as&nbsp;reflection typically comes with pretty hefty performance penalties and thus must be used sparsely where logging is absolutely needed. When reflection is used within the exception catching block, performance is usually not a concern. But we must pay attention to ensure not to cause further exceptions within the exception handling code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/05/25/event-logging-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reflection and Case Sensitivity</title>
		<link>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</link>
		<comments>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 02:48:56 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</guid>
		<description><![CDATA[Reflection is case sensitive by default, which comes at no surprise to most C# developers.However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do [...]]]></description>
			<content:encoded><![CDATA[<p>Reflection is case sensitive by default, which comes at no surprise to most C# developers.<span id="more-263"></span>However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do with the case sensitivity issue while using reflection.</p>
<p>I was doing some ASP.NET work and was using customized objects as data sources to bind to DataGrid. For sorting, I used the technique similar to what described in <a href="http://www.gridviewguy.com/ArticleDetails.aspx?articleID=203">Sorting GridView Using IComparer</a>. When I was testing my grids, however, I noticed that for most of the columns it worked perfectly, but for a few I somehow was greeted with the null reference error.</p>
<p>At first I thought that this was caused by the null items in those columns. After spending quite sometime digging, I realized that this was due to the case sensitivity issue in reflection. I had used the wrong case in the DataField of&nbsp; the BoundField. And after I changed the spelling to match that defined in the value object, the problem was solved.</p>
<p>VB developers should pay special attention when using reflections, as in VB.Net the language itself is not case sensitive.</p>
<p>As a side note, even though it is possible to set the binding flag to make the member lookup case insensitive, it is not recommended as <a href="http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx?loc=&amp;side=true">such approach tend to make the operation less efficient</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Event Handling Using Reflection</title>
		<link>http://www.kerrywong.com/2007/08/15/dynamic-event-handling-using-reflection/</link>
		<comments>http://www.kerrywong.com/2007/08/15/dynamic-event-handling-using-reflection/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 01:15:41 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://dimension/2007/08/15/dynamic-event-handling-using-reflection/</guid>
		<description><![CDATA[Download DynamicEvent.zip In an article earlier, I discussed how to dynamically load an assembly and execute it via the contract of a predefined interface. The technique mentioned there was useful for writing pluggable modules. However, any non-trivial implementations of a pluggable module probably require the communication between the module and the host program (e.g. progress, [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt;">Download <a href="/blog/wp-content/uploads/2007/09/dynamicevent.zip">DynamicEvent.zip</a></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;">In an <a href="/2007/07/06/yet-another-guide-to-dynamic-assembly-loading-and-execution-using-reflection/">article</a> earlier, I discussed how to dynamically load an assembly and execute it via the contract of a predefined interface. <span id="more-195"></span>The technique mentioned there was useful for writing pluggable modules. However, any non-trivial implementations of a pluggable module probably require the communication between the module and the host program (e.g. progress, status, etc). In the previous discussion, this capability was not present. <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;">To achieve dynamic event handling, we will first modify the PlugInInterface as follows:<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><o:p></o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">delegate</span></strong> <strong><span style="color: blue;">void</span></strong> <strong><span style="color: rgb(43, 145, 175);">ProgressChangedDelegate</span></strong>(<strong><span style="color: blue;">object</span></strong> state);<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">interface</span></strong> <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';"><span style="">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">event</span></strong> <strong><span style="color: rgb(43, 145, 175);">ProgressChangedDelegate</span></strong> ProgressChanged; <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">void</span></strong> Test();<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;">As shown above, an event is added to the interface (note, in VB.Net the delegate declaration is not required, the parameters can be implicitly supplied to the event ProgressChanged, e.g.</span><font size="3" face="Times New Roman"> <strong><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">Event</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> <strong><span style="color: rgb(43, 145, 175);">ProgressChanged</span></strong>(<strong><span style="color: blue;">ByVal</span></strong> state <strong><span style="color: blue;">As</span></strong> <strong><span style="color: blue;">Object</span></strong>)</span><span style="font-size: 10pt; font-family: Verdana;">).</span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><o:p></o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: Verdana;"><font size="3" face="Times New Roman">Next, I will add the event to the PlugInLib:<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><o:p></o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">class</span></strong> <strong><span style="color: rgb(43, 145, 175);">PlugIn</span></strong> : PlugInInterface<span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong> <o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">private</span></strong> <strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong> _ht;<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">event</span></strong> PlugInInterface<span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">ProgressChangedDelegate</span></strong> ProgressChanged;<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong> Hash<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">get</span></strong> {<strong><span style="color: blue;">return</span></strong> _ht;}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">set</span></strong> {_ht <span style="color: navy;">=</span> <strong><span style="color: blue;">value</span></strong>;}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> PlugIn()<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_ht <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: rgb(43, 145, 175);">Hashtable</span></strong>();<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_ht[<span style="color: rgb(163, 21, 21);">&quot;Key&quot;</span>] <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span>;<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">void</span></strong> Test()<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">if</span></strong> (ProgressChanged <span style="color: navy;">!=</span> <strong><span style="color: blue;">null</span></strong>)<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ProgressChanged(<span style="color: rgb(163, 21, 21);">&quot;start&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Console</span></strong><span style="color: navy;">.</span>WriteLine(_ht[<span style="color: rgb(163, 21, 21);">&quot;Key&quot;</span>]<span style="color: navy;">.</span>ToString());<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">if</span></strong> (ProgressChanged <span style="color: navy;">!=</span> <strong><span style="color: blue;">null</span></strong>)<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="">&nbsp;&nbsp;&nbsp;&nbsp;</span>ProgressChanged(<span style="color: rgb(163, 21, 21);">&quot;done!&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;  </span><o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: Verdana;">The changes in the main program are a bit more complex:<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><o:p></o:p></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">class</span></strong> <strong><span style="color: rgb(43, 145, 175);">Program</span></strong><span style="color: rgb(43, 145, 175);"><o:p></o:p></span></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">void</span></strong> Test()<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">string</span></strong> path <span style="color: navy;">=</span> <strong><span style="color: rgb(43, 145, 175);">Path</span></strong><span style="color: navy;">.</span>Combine(<strong><span style="color: rgb(43, 145, 175);">Environment</span></strong><span style="color: navy;">.</span>CurrentDirectory, <span style="color: rgb(163, 21, 21);">&quot;..\\..\\..\\PlugInLib\\bin\\debug&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">string</span></strong> fileName <span style="color: navy;">=</span> <strong><span style="color: rgb(43, 145, 175);">Path</span></strong><span style="color: navy;">.</span>Combine(path, <span style="color: rgb(163, 21, 21);">&quot;PlugInLib.dll&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Assembly</span></strong> asm <span style="color: navy;">=</span> <strong><span style="color: rgb(43, 145, 175);">Assembly</span></strong><span style="color: navy;">.</span>LoadFrom(fileName);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Type</span></strong> type <span style="color: navy;">=</span> asm<span style="color: navy;">.</span>GetType(<span style="color: rgb(163, 21, 21);">&quot;PlugInLib.PlugIn&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>PlugInInterface<span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong> cls <span style="color: navy;">=</span> <strong><span style="color: rgb(43, 145, 175);">Activator</span></strong><span style="color: navy;">.</span>CreateInstance(type) <strong><span style="color: blue;">as</span></strong> PlugInInterface<span style="color: navy;">.</span><strong><span style="color: rgb(43, 145, 175);">PlugInInterface</span></strong>;<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">EventInfo</span></strong> eventProgressChanged <span style="color: navy;">=</span> type<span style="color: navy;">.</span>GetEvent(<span style="color: rgb(163, 21, 21);">&quot;ProgressChanged&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Type</span></strong> eventDelegate <span style="color: navy;">=</span> eventProgressChanged<span style="color: navy;">.</span>EventHandlerType;<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Delegate</span></strong> d <span style="color: navy;">=</span> <strong><span style="color: rgb(43, 145, 175);">Delegate</span></strong><span style="color: navy;">.</span>CreateDelegate(eventDelegate, <strong><span style="color: blue;">this</span></strong>, <span style="color: rgb(163, 21, 21);">&quot;OnProgressChanged&quot;</span>);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">MethodInfo</span></strong> methodInfo <span style="color: navy;">=</span> eventProgressChanged<span style="color: navy;">.</span>GetAddMethod();<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Object</span></strong>[] args <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: blue;">object</span></strong>[] {d};<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>methodInfo<span style="color: navy;">.</span>Invoke(cls, args);<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>cls<span style="color: navy;">.</span>Test();<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp; </span><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="color: blue;">private</span></strong> <strong><span style="color: blue;">void</span></strong> OnProgressChanged(<strong><span style="color: blue;">object</span></strong> state)<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Console</span></strong><span style="color: navy;">.</span>WriteLine(state<span style="color: navy;">.</span>ToString());<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p>&nbsp;</o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: blue;">static</span></strong> <strong><span style="color: blue;">void</span></strong> <st1:place w:st="on">Main</st1:place>(<strong><span style="color: blue;">string</span></strong>[] args)<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="color: rgb(43, 145, 175);">Program</span></strong> p <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: rgb(43, 145, 175);">Program</span></strong>();<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>p<span style="color: navy;">.</span>Test();<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: 10pt; font-family: 'Courier New';"><font size="2" face="Courier New"><span style="">&nbsp;&nbsp;&nbsp; </span></font><font size="3" face="Times New Roman"><font size="2" face="Courier New">}</font><o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><o:p></o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Verdana"><span style="font-size: 10pt; font-family: Verdana;">To hook up the event from the dynamically loaded assembly, we first use Type.GetEvent to retrieve the EventInfo from PlugInLib.dll. Then, a delegate is created and hooked to the OnProgressChanged method within the current running assembly. Finally, methodInfo.Invoke(cls, args) links the delegates with the correctly the parameters with the event handler. <o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><font size="2" face="Verdana"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p></span></font></p>
<p><font size="2" face="Verdana"><span style="font-size: 10pt; font-family: Verdana;">The example shown here can be downloaded <a href="/blog/wp-content/uploads/2007/09/dynamicevent.zip">here</a>.</span></font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/08/15/dynamic-event-handling-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Populating Object from Text Files via Reflection</title>
		<link>http://www.kerrywong.com/2007/06/20/populating-object-from-text-files-via-reflection/</link>
		<comments>http://www.kerrywong.com/2007/06/20/populating-object-from-text-files-via-reflection/#comments</comments>
		<pubDate>Thu, 21 Jun 2007 00:41:01 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://dimension/2007/06/20/populating-object-from-text-files-via-reflection/</guid>
		<description><![CDATA[Download readfileviareflection.zip In .Net, an XML file or even a CSV file can easily be mapped to an object using proper attributes. Using the reflected object type, such XML or CSV files can easily be deserialized into the corresponding object. However, sometimes we do not have the luxury in specifying the format of the data [...]]]></description>
			<content:encoded><![CDATA[<p>Download <a title="readfileviareflection.zip" href="/blog/wp-content/uploads/2007/09/readfileviareflection.zip">readfileviareflection.zip</a></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">In .Net, an XML file or even a CSV file can easily be mapped to an object using proper attributes. Using the reflected object type, such XML or CSV files can easily be deserialized into the corresponding object.</span><span id="more-174"></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>However, sometimes we do not have the luxury in specifying the format of the data file and beyond the common XML and the less common CSV file format, we rarely use serialization technique to retrieve data from the file. For a concrete example, let&rsquo;s take a look at the following file that represents a free style inventory list:<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';">;&#8212;&#8212;&#8212;&#8212;&#8212;-<o:p></o:p> itemName =<span>  </span>Pen<o:p></o:p> quantity = 10<o:p></o:p> ;&#8212;&#8212;&#8212;&#8212;&#8212;-<o:p></o:p> itemName =<span>  </span>Paper<o:p></o:p> color = Red<o:p></o:p> size = A4<o:p></o:p> quantity = 100<o:p></o:p> ;&#8212;&#8212;&#8212;&#8212;&#8212;-<o:p></o:p> itemName = Box<o:p></o:p> size = Large<o:p></o:p> quantity = 20<o:p></o:p> ;&#8212;&#8212;&#8212;&#8212;&#8212;-<o:p></o:p> itemName = Ruler<o:p></o:p> length = long<o:p></o:p> quantity = 30<o:p></o:p> ;&#8212;&#8212;&#8212;&#8212;&#8212;-<o:p></o:p></span> <o:p> </o:p></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">As you can see from the list, each inventory item has a few properties that are a subset of all the available properties. Similar data structures can be found in other real world programs. For example, the INI file structure is similar to the example I gave. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>This kind of files is typically processed nonreflectively. We can however, still utilize some features with reflection to make a strongly typed object from the text file.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>The first step is to create a value object that holds all the possible keys (sample code in C#):<o:p></o:p></span></p>
<p class="MsoNormal"><o:p></o:p><span style="font-size: 10pt; font-family: 'Courier New';"><span>    </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">class</span></strong> <strong><span style="color: rgb(43, 145, 175);">InventoryItem</span></strong><span><o:p></o:p> </span>{<o:p></o:p> <span>        </span><strong><span style="color: blue;">private</span></strong> <strong><span style="color: blue;">string</span></strong> _itemName <span style="color: navy;">=</span> <strong><span style="color: blue;">null</span></strong>;<o:p></o:p> <span>        </span><strong><span style="color: blue;">private</span></strong> <strong><span style="color: blue;">string</span></strong> _color <span style="color: navy;">=</span> <strong><span style="color: blue;">null</span></strong>;<o:p> </o:p><span>            </span>. . .<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>        </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">string</span></strong> ItemName<o:p></o:p> <span>        </span>{<o:p></o:p> <span>            </span><strong><span style="color: blue;">get</span></strong> { <strong><span style="color: blue;">return</span></strong> _itemName; }<o:p></o:p> <span>            </span><strong><span style="color: blue;">set</span></strong> { _itemName <span style="color: navy;">=</span> <strong><span style="color: blue;">value</span></strong>; }<o:p></o:p> <span>        </span>}<o:p></o:p> <span>            </span>. . .<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>    </span>}</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Next we can use a parser to parse the text file line by line and use reflection to populate the class members and thus obtain a strongly-typed object. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>        </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">void</span></strong> Parse()<o:p></o:p> <span>        </span>{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span>. . .<o:p></o:p> <span>            </span><strong><span style="color: blue;">string</span></strong> line <span style="color: navy;">=</span> <strong><span style="color: blue;">string</span></strong><span style="color: navy;">.</span>Empty;<o:p> </o:p> <span>            </span>line <span style="color: navy;">=</span> fileStream<span style="color: navy;">.</span>ReadLine();<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><strong><span style="color: blue;">do</span></strong><span><o:p></o:p> </span>{<o:p></o:p> <span>                </span><strong><span style="color: blue;">if</span></strong> (line <span style="color: navy;">==</span> <strong><span style="color: blue;">null</span></strong>) <strong><span style="color: blue;">break</span></strong>;<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>                </span><strong><span style="color: blue;">if</span></strong> (line<span style="color: navy;">.</span>Trim()<span style="color: navy;">.</span>IndexOf(<span style="color: rgb(163, 21, 21);">&quot;;&#8212;-&quot;</span>) <span style="color: navy;">==</span> <span style="color: teal;">0</span>)<o:p></o:p> <span>                </span>{<o:p></o:p> <span>                    </span><strong><span style="color: blue;">if</span></strong> (_content <span style="color: navy;">==</span> <strong><span style="color: blue;">null</span></strong>)<o:p></o:p> <span>                    </span>{<o:p></o:p> <span>                        </span>_content <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: rgb(43, 145, 175);">InventoryItem</span></strong>();<o:p></o:p> <span>                    </span>}<o:p></o:p> <span>                    </span><strong><span style="color: blue;">else</span></strong><span><o:p></o:p> </span>{<o:p></o:p> <span>                        </span>_items<span style="color: navy;">.</span>Add(_content);<o:p></o:p> <span>                        </span>_content <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: rgb(43, 145, 175);">InventoryItem</span></strong>();<o:p></o:p> <span>                    </span>}<o:p></o:p> <span>                </span>}<o:p></o:p> <span>                </span><strong><span style="color: blue;">else</span></strong><span><o:p></o:p> </span>{<o:p></o:p> <span>                    </span>AddMemberInformation(line);<o:p></o:p> <span>                </span>}<o:p></o:p> <span>                </span>line <span style="color: navy;">=</span> fileStream<span style="color: navy;">.</span>ReadLine();<o:p></o:p> <span>            </span>} <strong><span style="color: blue;">while</span></strong> (line <span style="color: navy;">!=</span> <strong><span style="color: blue;">null</span></strong>);<o:p> </o:p> <span>            </span>fileStream<span style="color: navy;">.</span>Close();<o:p></o:p> <span>        </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p> </o:p> <span>        </span><strong><span style="color: blue;">private</span></strong> <strong><span style="color: blue;">void</span></strong> AddMemberInformation(<strong><span style="color: blue;">string</span></strong> line)<o:p></o:p> <span>        </span>{<o:p></o:p> <span>            </span><strong><span style="color: blue;">string</span></strong>[] ay <span style="color: navy;">=</span> line<span style="color: navy;">.</span>Split(<span style="color: rgb(163, 21, 21);">&#8216;=&#8217;</span>);<o:p> </o:p> <span>            </span><strong><span style="color: rgb(43, 145, 175);">Type</span></strong> t <span style="color: navy;">=</span> _content<span style="color: navy;">.</span>GetType();<o:p> </o:p> <span>            </span><strong><span style="color: rgb(43, 145, 175);">FieldInfo</span></strong> field <span style="color: navy;">=</span> t<span style="color: navy;">.</span>GetField(<strong><span style="color: rgb(43, 145, 175);">String</span></strong><span style="color: navy;">.</span>Concat(<span style="color: rgb(163, 21, 21);">&quot;_&quot;</span>, ay[<span style="color: teal;">0</span>]<span style="color: navy;">.</span>Trim()), <strong><span style="color: rgb(43, 145, 175);">BindingFlags</span></strong><span style="color: navy;">.</span>Instance <span style="color: navy;">|</span> <strong><span style="color: rgb(43, 145, 175);">BindingFlags</span></strong><span style="color: navy;">.</span>NonPublic);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span>field<span style="color: navy;">.</span>SetValue(_content, ay[<span style="color: teal;">1</span>]<span style="color: navy;">.</span>Trim());<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>        </span>}<o:p></o:p></span></p>
<p><span style="font-size: 10pt; font-family: Verdana;">The trick used here is in procedure AddMemberInformation(). The private fields are retrieved from the InventoryItem class via Type.GetField. Note that in order to match the private variable names in InventoryItem class, &ldquo;_&rdquo; is concatenated with the variable name found in the text file. Also, fields retrieved via reflection are case-sensitive. This might surprise those VB.Net programmers as variables in VB.Net are generally case insensitive.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/06/20/populating-object-from-text-files-via-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remote Code Execution via Reflection</title>
		<link>http://www.kerrywong.com/2007/02/15/remote-code-execution-via-reflection/</link>
		<comments>http://www.kerrywong.com/2007/02/15/remote-code-execution-via-reflection/#comments</comments>
		<pubDate>Thu, 15 Feb 2007 23:48:49 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://dimension/2007/02/15/remote-code-execution-via-reflection/</guid>
		<description><![CDATA[Background In .Net, distributed computing can be done in a number of ways. The most widely used way is to use web services, where a remote client would call a publicly exposed web method on a server where the web service is hosted. When using web services, the code of the service method must be [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Background<o:p></o:p></span></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">In .Net, distributed computing can be done in a number of ways.</span><span id="more-143"></span><span style="font-size: 10pt; font-family: Verdana;"> <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The most widely used way is to use web services, where a remote client would call a publicly exposed web method on a server where the web service is hosted. When using web services, the code of the service method must be physically on the server where the service is hosted. In this scenario the client talks to the server via a proxy class that represents the web service on the server.<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Alternatively, we can use .Net remoting to execute code in a distributed environment. Typically, either TCP or HTTP channel is used to communicate object states between client and server. The main advantage of using remoting is that custom sinks can be utilized to incorporate sophisticated data manipulations before the data is sent and received. The amount of data transferred can also be shrunk to minimal if binary formatters are used and thus resulting in very little channel overhead as compared to SOAP messages that typically used in web services. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Object serialization transfers the state of an object, not the physical object itself however. For serialization to work, the object to be serialized needs to exist on both the client and the server. When the object gets serialized, the state of the object is transferred across the wire. And when the object is deserialized, the object on the server side gets created and then populated with the states from data (the serialized object) transferred from the client.<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">So, in both scenarios, whether using web services or remoting, the object that needs to be used remotely must reside on both ends (a proxy or the actual object).<o:p></o:p></span></p>
<p class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Autonomous Remote Code Execution<o:p></o:p></span></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Can we use just one copy (e.g. the client version of the object) of the object to achieve remote code execution in a distributed environment? As it turns out, we can do this using reflection. For a lack of a better name, I call this approach <em>autonomous remote code execution</em>.<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The idea here is to create an object on the client side, and physically ship the object over to the server side and the server will execute whatever code that is send over.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">I will demonstrate how this can be done using TCP socket. To make the discussion easier, we will use synchronized socket communication model here, the principals illustrated here apply also to asynchronized sockets.<o:p> </o:p></span></p>
<p class="MsoNormal"><strong><em><span style="font-size: 10pt; font-family: Verdana;">The Client<o:p></o:p></span></em></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The client code first creates a TCP socket and reads in whatever .Net application needs to be executed as binary bytes. Then the content is sent through the TCP socket.<o:p></o:p></span></p>
<p class="MsoNormal">&nbsp;</p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font size="2" style="font-size: 9pt;"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System;</span></font></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Collections</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Generic;</span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Text;</span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Net;</span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Net</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Sockets;</span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">IO;</span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>namespace</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> SocketClient</span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000"><font face="Courier New, monospace"><font size="2">{</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">    </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>class</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Program</span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">    <font face="Courier New, monospace"><font size="2">{</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">        </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>public</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>void</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Send()</span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        <font face="Courier New, monospace"><font size="2">{</font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">TcpClient socketForClient;</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>try</strong></span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">{</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">socketForClient </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> TcpClient(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;alpha&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">, </span></font></font><font color="#008080"><font size="2"><span lang="">10</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font><font color="#008000"><font size="2"><span lang="">//server name </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">}</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>catch</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;Failed to connect to server&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>return</strong></span></font></font><font color="#000000"><font size="2"><span lang="">; </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">NetworkStream networkStream </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> socketForClient</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">GetStream(); </span></font></font><font color="#008000"><font size="2"><span lang="">//Read in the program to be sent </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>FileStream</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> fs </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>FileStream</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;PrimeGen.exe&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">, </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>FileMode</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Open); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryReader</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> br </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryReader</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(fs); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>byte</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] bin </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> br</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">ReadBytes(</span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>Convert</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">ToInt32(fs</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Length)); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">fs</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">br</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>StreamReader</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> streamReader </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>StreamReader</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(networkStream); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryWriter</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> streamWriter </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryWriter</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(networkStream); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>try</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>string</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> outputString; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">outputString </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> streamReader</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">ReadLine(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(outputString); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">streamWriter</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Write(bin); streamWriter</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Flush(); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">}</font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>catch</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;Exception reading from Server&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">networkStream</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close();</span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        </font></p>
<p style="margin-bottom: 0in;"><font color="#000000">        </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>static</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>void</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Main(</span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>string</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] args) { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">Program p </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Program(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">p</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Send(); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        <font face="Courier New, monospace"><font size="2">}</font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">    <font face="Courier New, monospace"><font size="2">}</font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font size="2"><font color="#000000"><span lang="">}</span></font></font></font></p>
<p class="MsoNormal"><strong><em><span style="font-size: 10pt; font-family: Verdana;">The Server<o:p></o:p></span></em></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">On the server side, a socket is created and then is blocked listening for the incoming connections. When the client and the server establish the connection, we read in the data as an array of bytes which represent the actual application transferred from the client side.<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">We then can get the actual object back using reflection. Finally, we can execute the code by jumping to its entry point. In this particular example, it is a .Net program called PrimeGen.<o:p></o:p></span><o:p> </o:p></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font size="2"><font color="#0000ff"><span lang=""><strong>using</strong></span></font><font color="#000000"><span lang=""> System; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Collections</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Generic; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Text; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Net; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Net</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Sockets; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Reflection; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>using</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> System</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">IO;</span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;">&nbsp;</p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>namespace</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> SocketServer { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">    </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>class</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Program { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">        </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>public</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>void</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Receive() { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">TcpListener tcpListener </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> TcpListener(Dns</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">GetHostEntry(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;alpha&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">)</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">AddressList[</span></font></font><font color="#008080"><font size="2"><span lang="">0</span></font></font><font color="#000000"><font size="2"><span lang="">], </span></font></font><font color="#008080"><font size="2"><span lang="">10</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">tcpListener</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Start(); Socket socketForClient </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> tcpListener</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">AcceptSocket(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>if</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> (socketForClient</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Connected) { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(</span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;Client connected&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">NetworkStream networkStream </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> NetworkStream(socketForClient); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>StreamWriter</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> streamWriter </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>StreamWriter</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(networkStream); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryReader</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> streamReader </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>BinaryReader</strong></span></font></font><font color="#000000"><font size="2"><span lang="">(networkStream); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>string</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> theString </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#a31515"><font size="2"><span lang="">&quot;Sending&quot;</span></font></font><font color="#000000"><font size="2"><span lang="">; streamWriter</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(theString); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(theString); streamWriter</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Flush(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//Note, here I simplified it a bit. Since I know that the </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//code is less than 16K, so I can just read everything in </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//at once. If the code size can not be predetermined, or it </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//is too large for a single transmittion, you will have to </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//slice it up and reassemble the data at the server side. </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>byte</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] bin </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> streamReader</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">ReadBytes(</span></font></font><font color="#008080"><font size="2"><span lang="">16384</span></font></font><font color="#000000"><font size="2"><span lang="">); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#008000"><font size="2"><span lang="">//load the assembly sent from the wire </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Assembly</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> a </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>Assembly</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Load(bin); </span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>MethodInfo</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> method </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> a</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">EntryPoint; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>if</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> (method </span></font></font><font color="#000080"><font size="2"><span lang="">!=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>null</strong></span></font></font><font color="#000000"><font size="2"><span lang="">) { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                    </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>object</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] obj </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>object</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[</span></font></font><font color="#008080"><font size="2"><span lang="">1</span></font></font><font color="#000000"><font size="2"><span lang="">] {</span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>string</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] {}}; </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                    </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>try</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                        <font face="Courier New, monospace"><font size="2"><span lang="">method</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Invoke(</span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>null</strong></span></font></font><font color="#000000"><font size="2"><span lang="">, obj); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                    <font face="Courier New, monospace"><font size="2"><span lang="">} </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>catch</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> (</span></font></font><font color="#2b91af"><font size="2"><span lang=""><strong>Exception</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> e) { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                        </font><font face="Courier New, monospace"><font color="#2b91af"><font size="2"><span lang=""><strong>Console</strong></span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">WriteLine(e</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Message); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">                    <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">streamReader</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">networkStream</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close(); </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">                <font face="Courier New, monospace"><font size="2"><span lang="">streamWriter</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Close(); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        <font face="Courier New, monospace"><font size="2">}</font></font></font></p>
<p lang="" style="margin-bottom: 0in;">&nbsp;</p>
<p style="margin-bottom: 0in;"><font color="#000000">        </font><font face="Courier New, monospace"><font color="#0000ff"><font size="2"><span lang=""><strong>static</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>void</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Main(</span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>string</strong></span></font></font><font color="#000000"><font size="2"><span lang="">[] args) { </span></font></font></font></p>
<p style="margin-bottom: 0in;"><font color="#000000">            <font face="Courier New, monospace"><font size="2"><span lang="">Program p </span></font></font><font color="#000080"><font size="2"><span lang="">=</span></font></font><font color="#000000"><font size="2"><span lang=""> </span></font></font><font color="#0000ff"><font size="2"><span lang=""><strong>new</strong></span></font></font><font color="#000000"><font size="2"><span lang=""> Program(); p</span></font></font><font color="#000080"><font size="2"><span lang="">.</span></font></font><font color="#000000"><font size="2"><span lang="">Receive(); </span></font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">        <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p lang="" style="margin-bottom: 0in;"><font color="#000000">    <font face="Courier New, monospace"><font size="2">} </font></font></font></p>
<p style="margin-bottom: 0in;"><font face="Courier New, monospace"><font size="2"><font color="#000000"><span lang="">}</span></font></font></font></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p></span></p>
<p class="MsoNormal"><strong><em><span style="font-size: 10pt; font-family: Verdana;">Client Server in Action<o:p></o:p></span></em></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Here is a screen shot of the client and server in action:</span></p>
<p align="center" class="MsoNormal"><a title="Remote Execution Using Windows" href="/blog/wp-content/uploads/2007/09/windows.GIF"><img alt="Remote Execution Using Windows" src="/blog/wp-content/uploads/2007/09/windows.GIF" /></a></p>
<p align="center" class="MsoNormal">Remote Execution Using Windows</p>
<p align="left" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Using this autonomous remote execution method, we can easily run the server anywhere by simply copy the server side executable. Just for fun, here is a screen shot of the server running on a FreeBSD server and the client is running Windows XP.</span></p>
<p align="center" class="MsoNormal"><a title="Remote Execution using FreeBSD" href="/blog/wp-content/uploads/2007/09/freebsd.GIF"><img alt="Remote Execution using FreeBSD" src="/blog/wp-content/uploads/2007/09/freebsd.GIF" /></a></p>
<p align="center" class="MsoNormal">Remote Execution using FreeBSD</p>
<p align="left" class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Final Thoughts<o:p></o:p></span></strong></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The autonomous remote code execution method mentioned here can be used in various situations. For instance, when conducting numerical analysis in a distributed fashion, it would typically require setting up the same code on all the computers that participate in the computation. To ensure that the all the computers are setup with the correct version of the code can be quite tedious. Further, whenever the computation task is changed, all the code needs to be re-deployed again across all the machines.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Using the approach mentioned here, we can in theory just install the server version of the code on all the computational nodes. This needs not to be changed over time regardless of whatever the code needs to be run in a distributive fashion. A single machine can be setup to inject the code that needs to be executed collaboratively into all the computational nodes. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Like everything else, this method has its limitations as well. For instance, the entire server instances need to run at a privileged level, otherwise the reflection portion of the server code might fail. Because the server code runs whatever the client tells it to run, running under privileged accounts can be potentially dangerous. However, for scientific computations, this should not be an issue since all the computational nodes are typically contained behind a firewall.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>The size of the code needs to be run remotely should be small enough for this method to be efficient, as the bandwidth could become a bottle neck if the code size is big or the number of computational nodes is large or both.<o:p></o:p></span></p>
<p><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>While I only demonstrated running a very simple test program remotely, it is possible to run programs that contain many DLLs and span multiple assemblies. Due to the limitation of our in-memory representation of the code on the server side however, it is mandatory that all assemblies be combined into a single executable before it can be used with the method mentioned here. This can be achieved by using <strong><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=22914587-b4ad-4eae-87cf-b14ae6a939b0&amp;DisplayLang=en">ILMerge</a> </strong> when working with Microsoft .Net implementation or <strong>mkbundle</strong> when using <a href="http://www.mono-project.com/Main_Page">Mono</a>.</span></p>
<p align="left" class="MsoNormal">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/02/15/remote-code-execution-via-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML Serialization Surprise in VS 2005</title>
		<link>http://www.kerrywong.com/2006/11/04/xml-serialization-surprise-in-vs-2005/</link>
		<comments>http://www.kerrywong.com/2006/11/04/xml-serialization-surprise-in-vs-2005/#comments</comments>
		<pubDate>Sat, 04 Nov 2006 10:11:30 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[Serialization]]></category>
		<category><![CDATA[Visual Studio .Net]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://dimension/2006/11/04/xml-serialization-surprise-in-vs-2005/</guid>
		<description><![CDATA[In a post last year, I wrote about the pitfalls when generating classes using the xsd tool in .Net framework 1.1. And some preliminary tests had suggested that the issues of xsd not generating the correct serializable class had been fixed in framework 2.0. A recent project at a client site had proved this to [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="/2005/09/17/xml-serialization-surprise/"><font face="Verdana">post</font></a><font face="Verdana"> last year, I wrote about the pitfalls when generating classes using the xsd tool in .Net framework 1.1.<span id="more-110"></span> And some preliminary tests had suggested that the issues of xsd not generating the correct serializable class had been fixed in framework 2.0. A recent project at a client site had proved this to be wrong.</font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000000" face="Verdana"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000"><font face="Verdana">As it turned out, while the .net framework 2.0 solved some of the xml serialization problems that plagued developers using framework 1.0, the same problems manifest themselves in different ways in framework 2.0. And the symptom of this problem can be mysterious and hard to identify.<o:p></o:p></font></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000000" face="Verdana"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000"><font face="Verdana">In my recent consulting position, I was developing some code using Visual Studio 2005 to integrate with some third party web services. But when I added a web reference to the service and wrote a simple one line program to test it, I got this exception: (note, I have replaced the class name, project name and path with {…}, so that I don’t have to reveal any information that might be related to the project.)</font> </font></font></span></p>
<blockquote dir="ltr" style="margin-right: 0px">
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Server Error in &#8216;/{class name} Application.<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000080"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Unable to generate a temporary class (result=1).<br />
error CS0029: Cannot implicitly convert type &#8216;string&#8217; to &#8216;string[]&#8216;<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</font></font></span></p>
<p><font face="Verdana"><font color="#000080">Exception Details: System.InvalidOperationException: Unable to generate a temporary class (result=1).<br />
error CS0029: Cannot implicitly convert type &#8216;string&#8217; to &#8216;string[]&#8216;</font></font></p>
<p><font face="Verdana"><font color="#000080">Source Error: <o:p></o:p></font></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Line 59:<span>         </span><o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Line 60:<span>         </span>/// &lt;remarks/&gt;<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Line 61:<span>         </span>public {Method name}() {<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Line 62:<span>             </span>string urlSetting = System.Configuration.ConfigurationManager.AppSettings["WebReference.{service name}"];<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Line 63:<span>             </span>if ((urlSetting != null)) {<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><br />
<font color="#000080">Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project path}\d8d15552\e16aa6bd\App_WebReferences.kd_c-r9m.0.cs    Line: 61</font></font></span></p>
<p><font face="Verdana"><font color="#000080">Stack Trace: <o:p></o:p></font></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000080"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">[InvalidOperationException: Unable to generate a temporary class (result=1).<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">error CS0029: Cannot implicitly convert type 'string' to 'string[]&#8216;<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">]<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, CompilerParameters parameters, Evidence evidence) +1389<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, CompilerParameters parameters, Assembly assembly, Hashtable assemblies) +3416<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) +171<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type) +241<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Web.Services.Protocols.SoapClientType..ctor(Type type) +546<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Web.Services.Protocols.SoapHttpClientProtocol..ctor() +243<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>WebReference.{service name}..ctor() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{path name}\d8d15552\e16aa6bd\App_WebReferences.kd_c-r9m.0.cs:61<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>_Default.Test() in C:\Documents and Settings\kwong\My Documents\Visual Studio 2005\WebSites\{path name}\Default.aspx.vb:11<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>_Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\kwong\My Documents\Visual Studio 2005\WebSites\{project name}\Default.aspx.vb:7<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Web.UI.Control.OnLoad(EventArgs e) +88<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Web.UI.Control.LoadRecursive() +74<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080"><span>   </span>System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000080"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; text-align: center" align="center"><span style="font-size: 10pt; font-family: Arial"></span></p>
<hr align="center" color="silver" noshade="noshade" size="1" width="100%" /><font face="Verdana"><font color="#000080"> </font></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42<o:p></o:p></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000080"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000080">C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project name}\d8d15552\e16aa6bd\ App_WebReferences.kd_c-r9m.0.cs<o:p></o:p></font></font></span></p>
</blockquote>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000" face="Verdana">At first, this looked like a permission issue. Since I had run into similar asp.net related permission issues before, this assumption sounded reasonable. After a quick search of internet, I found these two articles (</font><a href="http://support.microsoft.com/kb/896181"><span style="color: windowtext; text-decoration: none"><font face="Verdana">http://support.microsoft.com/kb/896181</font></span></a><font color="#000000"><font face="Verdana">, <a href="http://support.microsoft.com/kb/908158">http://support.microsoft.com/kb/908158</a>) on Microsoft’s website, and both articles suggested that I had some permission issue, even though the errors were not quite the same.<o:p></o:p></font></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000000" face="Verdana"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000"><font face="Verdana">But after double checking my permission settings and trying on different machines, I was convinced that this was caused by permissions. So what could it be?<o:p></o:p></font></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000000" face="Verdana"> </font></font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000"><font face="Verdana">I tried to make sense of the error message, and the line “Cannot implicitly convert type &#8216;string&#8217; to &#8216;string[]&#8216;” all of a sudden made me believe that this was due to similar issues as I experienced with Visual Studio 2003. Because by default Visual Studio 2005 does not generate the stub class and put it with the web project, but rather dynamically generates it during compile time (because of this, you see file name like “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project name}\d8d15552\e16aa6bd\ App_WebReferences.kd_c-r9m.0.cs” in the error message.), the only way we might be able to correct this problem is to generate the proxy ourselves and forget about trusting the IDE to generate the class for you.<o:p></o:p></font></font></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Arial"><o:p><font face="Verdana"><font color="#000000" face="Verdana"> </font></font></o:p></span></p>
<p><span style="font-size: 10pt; font-family: Arial"><font face="Verdana"><font color="#000000"><font face="Verdana">So I used wsdl.exe to generate the proxy, and sure enough, when I inspected the generated class, I found incorrect use of jagged arrarys ([][]) at places where a regular one dimensional arrays were needed. And after correcting these problems, I was able to consume the web service. By the way, when you use wsdl to generate the web service proxy you only need to include the generated class in your project and do not need to worry about adding the web reference, the proxy class is the reference to the web service.</font></font></font></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/11/04/xml-serialization-surprise-in-vs-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
