<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-38481552</id><updated>2012-03-20T00:56:00.810-07:00</updated><title type='text'>Nate's Programming Corner</title><subtitle type='html'>Software Development Comments, How-Tos, Examples, etc.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-38481552.post-1689468998275272050</id><published>2010-10-15T21:58:00.000-07:00</published><updated>2010-10-15T22:06:06.128-07:00</updated><title type='text'>I have a programming blog?</title><content type='html'>Somehow in the world of corporate software development, it is easy to lose track of things.  In my case, as a developer, I attempted various methods to deal with this, including trying out PIMs, writing my own info managers, and electronic sticky notes.  In moments of desperation I even resorted to occasional real sticky notes, rediscovering the existence of pen and paper, in the process.&amp;nbsp; At one point I remember looking at my desk and my two monitors with 38 different tabbed windows opened and realizing I needed something to organize my organizers.  This made me feel a bit heated, and as a result I somehow lost track of this particular blog.  If none of the preceding makes any logical sense to you, well, it doesn't to me either.&amp;nbsp; My fingers type, things come out on the screen.&amp;nbsp; It is the summary of my life in general thus far.&lt;br /&gt;&lt;br /&gt;While I was gone, Blogger.com reformatted their blogging site with newer, prettier design templates which broke the custom ones I used to encapsulate my code samples.  Even more surprising was to find that a few people had actually found this blog and commented on it, which made me feel bad for not responding, but then I had more intended it as a place for me to look up things I had figured out and then forgotten, than really thinking it might prove of interest to anyone else, or even found for that matter.&amp;nbsp; Surely this blog only comes up on page 5052 on Google.&lt;br /&gt;&lt;br /&gt;So I apologize for not getting back to some of the comments.&amp;nbsp; Alas, technology (especially web development) has moved forward, and much of this information is becoming rather irrelevant.&amp;nbsp; Silverlight is now 4.0 and a rather sleek animal compared to the rough beast that was SL 2.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In any event, times have changed considerably since my last post here, and I am starting a new blog, supported by a CMS that I can manage and modify as needed to keep the content up to date and track what I am doing and learning and those times when the little light-bulb appears above my head.&amp;nbsp; Perhaps only a programmer/hacker would choose a more sophisticated platform to make things simpler, but I really think it will.&amp;nbsp; More information forthcoming, with a link, in my next (and probably last) post to this blog.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-1689468998275272050?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/1689468998275272050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=1689468998275272050' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/1689468998275272050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/1689468998275272050'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2010/10/i-have-programming-blog.html' title='I have a programming blog?'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-4003646980730882904</id><published>2009-02-04T10:34:00.000-08:00</published><updated>2009-02-04T10:38:35.919-08:00</updated><title type='text'>Compact Coding: The Conditional Ternary ? Operator in C#</title><content type='html'>When I first came across the ? operator in C# I didn't know what I was looking at, which is probably why &lt;i&gt;some&lt;/i&gt; C# programmers like to use it (ahh the power of esoteric knowledge!)  However, as I've come to appreciate more the economy of some C# conventions, I find it very convenient at times.  For those unfamiliar with it, the ? operator (known as a ternary operator) is a sort of short hand for assignment statements based on a condition.  The basic syntax is:&lt;br /&gt;[variable name] = [boolean or condition] ? [value to assign if condition is true] : [value to assign if condition is false]&lt;br /&gt;&lt;br /&gt;For example, this code:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (x == 0)&lt;br /&gt;{&lt;br /&gt;    y = 1;&lt;br /&gt;}&lt;br /&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;    y = x;&lt;br /&gt;}&lt;/pre&gt;can be shortened to this statement:&lt;pre class="csharpcode"&gt;&lt;br /&gt;y = (x == 0) ? 1 : x;&lt;/pre&gt;This may beg the question of code readability, but this has never been as high a priority in C# as in VB.  C# developers tend to prefer tight, efficient code.  Take, for example, the newer method available for declaring properties:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; name;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; name;&lt;br /&gt;    }&lt;br /&gt;    set&lt;br /&gt;    {&lt;br /&gt;        name = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;can now be written as&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;/pre&gt;and I can tell you that I use the latter whenever possible.  This isn't a knock against VB (I was a VB programmer for many years before switching over to primarily writing code in C#) but simply an observation of one of the different approaches of the two languages.&lt;br /&gt;&lt;br /&gt;In any event,the ternary ? operator can be an effective technique for tightening up your code in the appropriate circumstances.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-4003646980730882904?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/4003646980730882904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=4003646980730882904' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/4003646980730882904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/4003646980730882904'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2009/02/compact-coding-conditional-ternary.html' title='Compact Coding: The Conditional Ternary ? Operator in C#'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-8733622352721138276</id><published>2009-02-02T13:39:00.000-08:00</published><updated>2009-02-03T14:37:25.483-08:00</updated><title type='text'>Creating and Installing a Windows service using Visual Studio 2008</title><content type='html'>There are a large number of sources on the internet describing how to create and/or install a Windows service in .Net, but as I was developing one for the company I work for I did not find one that comprehensively covered everything I needed to know so I thought I'd write a blog entry on this topic.  (Note: If you have trouble reading any of the screen shots below, click on the screen shot for a larger image.)&lt;br /&gt;&lt;br /&gt;Creating the service in Visual Studio 2008 is relatively simple.  To do so, create a new project using the Windows Service template:&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYdp-KFeQOI/AAAAAAAAAAo/e2pBAVb4LE0/s1600-h/WindowsServiceDialog1.JPG"&gt;&lt;img style="margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 291px;" src="http://4.bp.blogspot.com/_etej9HVX1To/SYdp-KFeQOI/AAAAAAAAAAo/e2pBAVb4LE0/s400/WindowsServiceDialog1.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298320003204792546" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once you have the project in your solution explorer, right click on the service (named Service1.cs by default) and select View Code.  This will bring up a skeleton class for your service where you can add code to execute in the OnStart and OnStop methods for when your service starts and stops, respectively.&lt;br /&gt;&lt;br /&gt;Once I got this far I went ahead and wrote my code and was ready to begin debugging it.  However, as you may already know, you cannot simply run a service in debug mode from Visual Studio.  You have to install it, get it running, and then attach to it as a process.  I googled on how to install a service and found &lt;a href="http://msdn.microsoft.com/en-us/library/sd8zc8ha.aspx"&gt;this page&lt;/a&gt; on MSDN where it said to access the directory in which your project's compiled executable file is located and run InstallUtil.exe from the command line (i.e. installutil yourproject.exe).  I tried this and got the message 'installutil' is not recognized as an internal or external command, operable program or batch file.  Microsoft forgot to mention that installutil.exe is not part of the environment path by default, so I had to hunt it down.  Doing a file search, I found it in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ directory.  Anticipating that I would be running it more than once, I added this to the command line path so that I could execute it from anywhere in my file system.  To do this, right click on My Computer in Windows explorer, select Properties, click on the Advanced tab, click on the Environment Variables button, select the Path variable, click on the Edit button, add a semicolon to the existing Path value, followed by "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\;" and click the OK button.&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYdyC7TTI7I/AAAAAAAAAAw/gJP7KuhQCts/s1600-h/PathDialog.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand" src="http://4.bp.blogspot.com/_etej9HVX1To/SYdyC7TTI7I/AAAAAAAAAAw/gJP7KuhQCts/s400/PathDialog.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298328881228620722" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Okay, that wasn't so bad, I thought.  I brought up the command prompt, changed to the directory where my exe file was being built, and Windows now knew what installutil was when I typed it in.  So, following my instructions from MSDN, I typed in "installutil myService.exe".  This generated some output in the command window which I largely ignored other than the last few lines which seemed to indicate the install was successful.  However, when I went to look at the services running on my machine my service wasn't there.  I went back to my command window and read the output more carefully, and saw "No public installers with the RunInstallerAttribute.Yes attribute could be found".  What the heck?  &lt;br /&gt;&lt;br /&gt;After a little more research, I discovered I needed to add an installer to my project in order for the command line utility to work.  This is easy enough to do once you know about it - simply bring up your service designer and right click somewhere on the gray background area(if you have added any components, make sure you are selecting the service itself and not a component) and then select Add Installer.  This adds an installer class with two components - a service installer and a service process installer.  If you click on the service installer component and look at the properties, you can see where you can name your service (using the ServiceName property) and specify whether to start it automatically or manually (in the StartType property).  Next click on the service process installer component and in its properties you can specify what account to run your service under via the Account property (if you select user you will need to provide the name and password of the user the service will run under).  I selected Local System as I didn't want to have to supply credentials and I knew this would have access rights to the file system by default.  (For some applications, this may not be the best choice if security is an issue, but you should also be aware that the other choices of LocalService or NetworkService may not have the access needed to actually run your service by default.)&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYd6F-CnJ5I/AAAAAAAAABA/7kWVfwJ6rqI/s1600-h/InstallerDialog.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 211px;" src="http://4.bp.blogspot.com/_etej9HVX1To/SYd6F-CnJ5I/AAAAAAAAABA/7kWVfwJ6rqI/s400/InstallerDialog.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298337729596565394" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once I had done all of this, I did another build of my project and then went back to the command line.  Installutil worked.  I could see the service in my list of services.  I was happy - until I realized my service wasn't working right.  It turned out that this was because I was using an App.config file to initialize some settings.  After a little more research, I discovered I needed to copy this to the location of the executable that was being run as the service and rename it to the name of the executable but with a .config extension (i.e. myService.exe.config.)  I did this, stopped and restarted the service, and all was well.  &lt;br /&gt;&lt;br /&gt;After some reflection, however, I thought "Why do I have to start the service manually after I install it?  Is there a way to have it start automatically when it is installed?"  It turns out there is a way, and it is actually pretty simple.  To have your service start upon installation, bring up the code-behind for your project installer (right-click on it in Solution Explorer and select View Code).  You should see something like this:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Configuration.Install;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; myService&lt;br /&gt;{&lt;br /&gt;    [RunInstaller(&lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ProjectInstaller : Installer&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ProjectInstaller()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;What we want to do here is have the Installer class start up our service after installation.  It turns out the Installer class has an event called Committed that occurs after a successful installation.  All that is needed is to add an event handler for this event and put code in it to start the service.  To add the event handler, add this line of code after InitializeComponent():&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Committed += &lt;span class="kwrd"&gt;new&lt;/span&gt; InstallEventHandler(ProjectInstaller_Committed);&lt;/pre&gt;and this new function to your class:&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; ProjectInstaller_Committed(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, InstallEventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;(Intellisense will do most of the work for you if you hit the TAB key at the appropriate times.)  Now we just need to add the code to actually start the service when this event occurs.  First add this namespace to your using statements:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceProcess;&lt;/pre&gt;Next, in your event handler function, add this code:&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;ServiceController sc = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceController(&lt;span class="str"&gt;"myService"&lt;/span&gt;);&lt;br /&gt;sc.Start();&lt;/pre&gt;(Replace "myService" with your actual service name.)  When you are done, your project installer class should look something like this:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ComponentModel;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Configuration.Install;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceProcess;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; myService&lt;br /&gt;{&lt;br /&gt;    [RunInstaller(&lt;span class="kwrd"&gt;true&lt;/span&gt;)]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ProjectInstaller : Installer&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ProjectInstaller()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Committed += &lt;span class="kwrd"&gt;new&lt;/span&gt; InstallEventHandler(ProjectInstaller_Committed);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;void&lt;/span&gt; ProjectInstaller_Committed(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, InstallEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            ServiceController sc = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceController(&lt;span class="str"&gt;"myService"&lt;/span&gt;);&lt;br /&gt;            sc.Start();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;When you are done, save and build your project.  You should now find that upon installation, your service is started automatically.  This was a plus for me because it was one less set of instructions that I needed to supply to our deployment group.  Along these lines, I began thinking about what else I could do to make deployment simpler, and decided to go ahead and add a setup project to my solution.  This would mean (in theory) that all that would be needed to install the service would be to double-click on an msi file rather than mucking about with the command prompt and installutil.  To add a setup project for your service, right-click on the solution in Solution Explorer and select Add-&gt;New Project.  From the project templates, select Setup Project (usually under Other Project Types-&gt;Setup and Deployment in VS 2008.)&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYheK9AO8YI/AAAAAAAAABI/aqGHNjUq4I0/s1600-h/SetupDialog.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 262px;" src="http://4.bp.blogspot.com/_etej9HVX1To/SYheK9AO8YI/AAAAAAAAABI/aqGHNjUq4I0/s400/SetupDialog.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298588503868633474" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once you have added a setup project to your solution, you need to add a Custom Action to your setup project so that it knows what to install.  To do this, right-click on your setup project in Solution Explorer and select View-&gt;Custom Actions.  This will bring up the Custom Actions window.  Right click on Custom Actions in this window and select Add Custom Action.  This will bring up a dialog box.  Double-click the Application Folder and click on the Add Output... button.  This brings up another dialog box.  It should already have the primary output for your service project selected using the Active configuration, so just click OK and then OK again.&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYhgWIsZ2WI/AAAAAAAAABQ/N8E3WnpVgm8/s1600-h/CustomActionDialog.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 331px; height: 400px;" src="http://4.bp.blogspot.com/_etej9HVX1To/SYhgWIsZ2WI/AAAAAAAAABQ/N8E3WnpVgm8/s400/CustomActionDialog.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298590895008504162" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once you have done so, it will add the necessary Custom Action to the setup project to install your service.&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_etej9HVX1To/SYhhPHaBkzI/AAAAAAAAABY/FzlSnjrven0/s1600-h/CustomActionAdded.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 222px;" src="http://4.bp.blogspot.com/_etej9HVX1To/SYhhPHaBkzI/AAAAAAAAABY/FzlSnjrven0/s400/CustomActionAdded.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298591873915523890" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once you have done this, build your setup project.  Assuming everything has been done correctly, once the build completes you will find an msi file in your ouput directory.  If you installed your service manually using installutil then uninstall it the same way (with the /u switch).  Double-click on the msi file in Windows explorer and a little setup wizard will guide you through the installation process of your service.  If you have an app.config file, it will even rename and move this for you, rather than having to do it manually as with using installutil. &lt;br /&gt;&lt;br /&gt;I did this and thought "great! I'm done!" but something was nagging at me.  It was the fact that the setup wizard had an option to install the service for everyone or just me, and was defaulting to just me.  I wanted this service to be installed for everyone when it was deployed.  In the interests of further simplifying deployment instructions and eliminating another hazard, I wanted this to default to everyone.  If you want to do this, it is fairly simple.  Select the setup project in Solution Explorer and look at the properties (F4 brings up the properties window if you don't see it).  There is a property called InstallAllUsers that by default is set to false.  Set it to true, and when you run the setup project, it will default to everyone rather than just me.  After further reflection I decided that I wanted to remove the option altogether, since I wanted to always install for everyone.  If you want to do this, right-click on your setup project in Solution Explorer and select View-&gt;User Interface.  This will bring up the user interface window, which is basically a tree-view of the setup screens the user will see.  If you click on Installation Folder in this window and look at the properties, you will see a property called InstallAllUsersVisible.  If you set this to false, the setup project will not show this option.&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_etej9HVX1To/SYhpI0h_UhI/AAAAAAAAABg/pEpG4tQBxJo/s1600-h/RemoveAllUsersDialog.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 205px;" src="http://1.bp.blogspot.com/_etej9HVX1To/SYhpI0h_UhI/AAAAAAAAABg/pEpG4tQBxJo/s400/RemoveAllUsersDialog.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298600561862464018" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I was pretty happy at this point.  Deploying the service couldn't be simpler.  I tested it myself.  I installed it.  I double-clicked the msi file again and was able to uninstall it, but I noticed there was also an option to repair.  I found that if I selected repair rather than uninstall, I would get a message saying "Error 1001.  The specified service already exists".  This was not a show-stopper, but it was annoying.  I don't like sending out applications for deployment knowing they can generate an error.  What if the config file was deleted or changed it such a way that the service didn't work right?  What if the service was removed manually?  Sure the user could uninstall and then reinstall the service, but it would be much nicer if they could choose repair and it would work rather than generating an error.&lt;br /&gt;&lt;br /&gt;It turns out the reason for the error is that when selecting repair, it was trying to install the service again even if it was already installed.  If the service was already on the machine, I didn't want repair to try to put it on there again and then generate an error.  To fix this, you have to go back to your Custom Actions screen (right-click on your setup project in Solution Explorer, select View-&gt;Custom Actions.)  Under the install folder, select the Primary Output for your service and look at its properties.  There is one called Condition.  Set this property's value to Not Intalled (this is case sensitive.)&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_etej9HVX1To/SYht4Cr-ceI/AAAAAAAAABo/3QOCmcq5HGk/s1600-h/NotInstalled.JPG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 231px;" src="http://1.bp.blogspot.com/_etej9HVX1To/SYht4Cr-ceI/AAAAAAAAABo/3QOCmcq5HGk/s400/NotInstalled.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5298605771162808802" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now the setup package knew not to install the service if it was already there, but I was still getting an error when I tried to run setup with the repair option.  It was saying that it cannot start the service because an instance of the service is already running.  I needed to go back to the code I added earlier to the project installer to automatically start the service and only have it do this if the service was stopped.  To do this, I brought up the code-behind for my project installer, and modified the event handler as follows:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; ProjectInstaller_Committed(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, InstallEventArgs e)&lt;br /&gt;{&lt;br /&gt;    ServiceController sc = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceController(&lt;span class="str"&gt;"myService"&lt;/span&gt;);&lt;br /&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (sc.Status == ServiceControllerStatus.Stopped)&lt;br /&gt;    {&lt;br /&gt;        sc.Start();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;After doing this, I did a build of my service project and a build of my setup project (note that if you do a build on your solution, it will not build the setup project by default.)  Using the repair option when running setup now worked as desired and did not generate any errors.  I was done with setting up the installation of the service.&lt;br /&gt;&lt;br /&gt;Back to the debug issue.  Now that I had the service installed, I wanted to hit some breakpoints in my code.  In order to do this, make sure your service is running.  Then while your project is up in Visual Studio, select Debug-&gt;Attach to process.  Check the Show processes from all users checkbox (otherwise you will only see a grayed-out, disabled process with vshost as part of your service name which you cannot attach to.)  Find your service name in the list of processes, select it, and click the Attach button.  You will now be able to hit breakpoints in your service (although you may not be able to hit breakpoints put directly in the OnStart method due to timing issues.)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-8733622352721138276?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/8733622352721138276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=8733622352721138276' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/8733622352721138276'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/8733622352721138276'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2009/02/creating-and-installing-windows-service.html' title='Creating and Installing a Windows service using Visual Studio 2008'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_etej9HVX1To/SYdp-KFeQOI/AAAAAAAAAAo/e2pBAVb4LE0/s72-c/WindowsServiceDialog1.JPG' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-3735640521266160002</id><published>2009-01-08T07:20:00.000-08:00</published><updated>2009-01-08T07:27:30.039-08:00</updated><title type='text'>Using a Timer in Silverlight 2.0</title><content type='html'>Earlier versions of Silverlight did not have a timer class but now in 2.0 one is available.  The class is called DispatcherTimer and is located in the System.Windows.Threading namespace.  Other than this it behaves just like a regular .NET timer, as the code sample below will show.&lt;br /&gt;&lt;br /&gt;First add a reference to the namespace at the top of your code-behind page:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Windows.Threading;&lt;/pre&gt;&lt;br /&gt;Then you can initialize the timer and start it wherever you want:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="rem"&gt;// Declare the timer variable&lt;/span&gt;&lt;br /&gt;DispatcherTimer timer = &lt;span class="kwrd"&gt;new&lt;/span&gt; DispatcherTimer();  &lt;br /&gt;&lt;span class="rem"&gt;// Set the interval in milliseconds (the example below sets the interval to 1 second)&lt;/span&gt;&lt;br /&gt;timer.Interval = &lt;span class="kwrd"&gt;new&lt;/span&gt; TimeSpan(0, 0, 0, 0, 1000);&lt;br /&gt;&lt;span class="rem"&gt;// Now add an event handler for when the timer ticks (intellisense can save you some typing here)&lt;/span&gt;&lt;br /&gt;timer.Tick += &lt;span class="kwrd"&gt;new&lt;/span&gt; EventHandler(timer_Tick);&lt;br /&gt;&lt;span class="rem"&gt;// Everything is set, and you can now start your timer&lt;/span&gt;&lt;br /&gt;timer.Start();&lt;/pre&gt;&lt;br /&gt;Don't forget this is running asynchronously so you may want to stop the timer and then start it again when your code evoked by the timer is complete, as demonstrated in the timer tick event error handler below (In this example, the myFunction function will be called on one second intervals):&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; timer_Tick(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{  &lt;br /&gt;    ((DispatcherTimer)sender).Stop();&lt;br /&gt;    myFunction();&lt;br /&gt;    ((DispatcherTimer)sender).Start();&lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-3735640521266160002?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/3735640521266160002/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=3735640521266160002' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/3735640521266160002'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/3735640521266160002'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2009/01/using-timer-in-silverlight-20.html' title='Using a Timer in Silverlight 2.0'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-890933765688011112</id><published>2009-01-07T08:12:00.000-08:00</published><updated>2009-01-07T08:42:22.544-08:00</updated><title type='text'>Consuming a Web Service in Silverlight</title><content type='html'>Consuming a web service in Silverlight is a little different than in a standard web application. Assuming you are using the Visual Studio IDE with Silverlight installed, when you create a service reference in your Silverlight project, some methods and events are created for that reference so that the call to the web service method can be (and actually &lt;i&gt;must&lt;/i&gt; be) done asynchronously.  This can be a little confusing if you don't know what is going on.  For example, when you instantiate your service reference to the web service, you won't see the web method names that you might be expecting.  They will all be there, but they will have 'Async' added to the end of the name.  Also, since the calls are asynchronous, the code will not wait for the return data before continuing execution.  This means you have to setup an Event Handler for when the call is completed and returns your data.&lt;br /&gt;&lt;br /&gt;This is what your code will look like to instantiate the service and setup the event handler (luckily intellisense will do most of the typing for you if you hit TAB when appropriate):&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;MyServiceReference.MyServiceNameSoapClient mySvc = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyServiceReference.MyServiceNameSoapClient();&lt;br /&gt;mySvc.myWebMethodCompleted += &lt;span class="kwrd"&gt;new&lt;/span&gt; EventHandler&amp;lt;myNameSpace.MyServiceReference.myWebMethodCompletedEventArgs&amp;gt;(mySvc_myWebMethodCompleted);&lt;/pre&gt;&lt;br /&gt;now you are ready to call your web method &lt;i&gt;asynchronously&lt;/i&gt;:&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;mySvc.myWebMethodAsync();&lt;/pre&gt;&lt;br /&gt;The final step is to handle the return data once the call has completed in an Event Handler as specified above.  The handler's EventArgs parameter will contain the results of your web method call in its Result property:&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; mySvc_myWebMethodCompleted(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, myNameSpace.myServiceReference.myWebMethodCompletedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            myTextBlock.Text = e.Result.ToString();&lt;br /&gt;        }&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-890933765688011112?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/890933765688011112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=890933765688011112' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/890933765688011112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/890933765688011112'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2009/01/consuming-web-service-in-silverlight.html' title='Consuming a Web Service in Silverlight'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-831154745039953726</id><published>2009-01-06T10:53:00.000-08:00</published><updated>2009-02-03T09:15:28.071-08:00</updated><title type='text'>Configuring IIS with Mime Types for Silverlight</title><content type='html'>Deploying a Silverlight app is pretty simple. Just put the compiled assembly (and any resources your app depends on) on the web server, and map a virtual directory (under the default website) to it in IIS. If you have done this and it still isn't working, IIS may not have the mime types needed for WCF/Silverlight. This is easy to fix. Just add the following mime maps to the default website in IIS:&lt;/p&gt;&lt;style type="text/css"&gt;.nobrtable br { display: none }&lt;/style&gt;&lt;br /&gt;&lt;div class="nobrtable"&gt;&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;strong&gt;&lt;span style="color:#000099;"&gt;Extension&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;&lt;span style="color:#000099;"&gt;Content Type&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.xaml&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/xaml+xml&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.xap &lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/x-silverlight-app&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.xbap&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/x-ms-xbap&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;For IIS 5 and 6 you can do this by bringing up the properties of the default website, click on the Http Headers tab, and then click on the File Types button under "Mime Mapping".&lt;br /&gt;&lt;br /&gt;Note - if you want to add all mime types used by WPF, add these as well:&lt;br /&gt;&lt;div class="nobrtable"&gt;&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;strong&gt;&lt;span style="color:#000099;"&gt;Extension&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;&lt;span style="color:#000099;"&gt;Content Type&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.application&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/x-ms-application&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.deploy&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/octet-stream&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.manifest&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/manifest&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;.xps&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color:#000099;"&gt;application/vnd.ms-xpsdocument&lt;/span&gt;&lt;/td&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tr&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-831154745039953726?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/831154745039953726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=831154745039953726' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/831154745039953726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/831154745039953726'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2009/01/configuring-iis-with-mime-types-for.html' title='Configuring IIS with Mime Types for Silverlight'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-3640401148887734675</id><published>2008-12-29T12:31:00.000-08:00</published><updated>2008-12-29T14:20:41.186-08:00</updated><title type='text'>Accessing the NT Username in Silverlight 2.0</title><content type='html'>I found in a recent Silverlight application that I was working on that I needed to pass the username to a web service call, and that the standard methods I was accustomed to using in .NET were not available in the Silverlight application.  After a little research on the web and some trial and error, I was able to do this by retrieving this information in the web application that was hosting the Silverlight application control, and then using Silverlight application &lt;strong&gt;InitParams&lt;/strong&gt; to pass the data back to the Silverlight application.  (This seemed to me simpler and more elegant than the alternative method, that of creating a web service for the sole purpose of providing a username to Silverlight.)  Below is a step-by-step process on how to do this.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 1 - Retrieve the username in the web application hosting your Silverlight application&lt;/strong&gt;&lt;br /&gt;In order to do this, you will need a code-behind page for your page hosting the Silverlight control, (which is not created by default for the Silverlight aspx test page.)  To add this, first you need to modify the first line of your aspx page hosting the Silverlight app.  Initially it will look something like this:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="asp"&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" %&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;You will need to add the CodeBehind and Inherits attributes to this to point to the codebehind page that you will be adding.  Assuming you have just created a new Silverlight app and are using the pre-generated aspx test page, the modified first line will look something like as follows:&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="asp"&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SilverlightApplication1TestPage.aspx.cs" Inherits="SilverlightApplication1.Web.SilverlightApplication1TestPage" %&amp;gt;&lt;/span&gt;&lt;/pre&gt;Next we need to add the actual codebehind page that the above refers to.  To add this, right click on your web application project, select Add-&gt;New Item, select the Code File template, and give it the same name as the aspx page hosting your Silverlight app (in our example this would be SilverlightApplication1.aspx.cs).  This page will be blank by default, so we will want to add in the following to complete our tie-in with its aspx parent page and handle the page load event:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.UI;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.UI.WebControls;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SilverlightApplication1.Web.Web&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SilverlightApplication1TestPage : System.Web.UI.Page&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Page_Load(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;        {&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;In order to be able to reference the Silverlight control programmatically, we also need to declare it.  Assuming it has the default name of Xaml1, we would add a line below the class declaration as follows:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; global::System.Web.UI.SilverlightControls.Silverlight Xaml1;&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;Step 2 - Retrieve the Username&lt;/strong&gt;&lt;br /&gt;We can now retrieve the username using the standard .NET framework method.  To do this, we first add a line to the top of the codebehind page to reference the System.Security.Principal namespace:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Principal;&lt;/pre&gt;&lt;br /&gt;We can then add the code to retrieve the username using the WindowsIdentity.GetCurrent method in the Page Load event:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; userName = WindowsIdentity.GetCurrent().Name.ToString();&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;Step 3 - Pass the Username as an InitParameter to the Silverlight application&lt;/strong&gt;&lt;br /&gt;The Silverlight application object as defined by App.xaml and App.xaml.cs in the Silverlight app has a a startup event object called InitParams that is a dictionary of key value pairs and is empty by default.  To populate this programmatically with values in the hosting web application, we set the property of the Silverlight control called InitParameters to our key/value pair in the format of a string &lt;em&gt;"key=value"&lt;/em&gt;.  Continuing in our example, in the page load event under the line that retrieves the username as described above, we would add the following line of code:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Xaml1.InitParameters = String.Format(&lt;span class="str"&gt;"UserName={0}"&lt;/span&gt;, userName);&lt;/pre&gt;At this point we are done with the web hosting application and can now retrieve this value in the Silverlight app.  Our entire codebehind page for the hosting aspx page will now look something like this:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.UI;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.UI.WebControls;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Principal;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SilverlightApplication2.Web.Web&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SilverlightApplication2TestPage : System.Web.UI.Page&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; global::System.Web.UI.SilverlightControls.Silverlight Xaml1;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Page_Load(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; userName = WindowsIdentity.GetCurrent().Name.ToString();&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Xaml1.InitParameters = String.Format(&lt;span class="str"&gt;"UserName={0}"&lt;/span&gt;, userName);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;Step 4 - Retrieving the username via the InitParams collection in the Silverlight Application&lt;/strong&gt;&lt;br /&gt;When you open the App.xaml.cs codebehind page in the Silverlight application, you will see an auto-generated event handler for the Silverlight application startup event as follows:&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Startup(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, StartupEventArgs e)&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.RootVisual = &lt;span class="kwrd"&gt;new&lt;/span&gt; Page();&lt;br /&gt;}&lt;/pre&gt;In this event we can use StartupEventArgs to retrieve our username via the InitParams collection, but we need a place to store it, so we will create a property first, under the class declaration, as follows (note this uses the C# shortened notation for a simple property):&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; UserName { get; set; }&lt;/pre&gt;Now, going back to our Application_Startup event handler, we will add the line to populate this property with the username retrieved from the StartupEventArgs InitParams collection that we populated earlier on the web application side.  The syntax for this (following our example thus far) is as follows:&lt;pre class="csharpcode"&gt;&lt;br /&gt;UserName = e.InitParams[&lt;span class="str"&gt;"UserName"&lt;/span&gt;];&lt;/pre&gt;We can now reference this property in our Page.xaml.cs codebehind if needed by creating a reference to the current application object and then referencing our UserName property, as the following two lines of code illustrate:&lt;pre class="csharpcode"&gt;&lt;br /&gt;App currentApp = ((App)Application.Current);&lt;br /&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; userName = currentApp.UserName;&lt;/pre&gt;and Voila!  In my case, I was now able to pass that username as a parameter to a web service call from the codebehind of my Page.xaml in my Silverlight application.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-3640401148887734675?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/3640401148887734675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=3640401148887734675' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/3640401148887734675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/3640401148887734675'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2008/12/accessing-nt-username-in-silverlight-20.html' title='Accessing the NT Username in Silverlight 2.0'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-38481552.post-116787475867519568</id><published>2007-01-03T17:07:00.000-08:00</published><updated>2009-03-11T08:16:12.923-07:00</updated><title type='text'>Deep Copy - Cloning in VB .NET</title><content type='html'>Needing to be able to clone an object is a common requirement when working in .NET, since simply assigning an object instance to a new variable name just adds a reference pointer to the existing object and sometimes you need to be able to create a new instance that is an exact copy of the existing one.&lt;br /&gt;&lt;br /&gt;For many of the existing classes in .NET, the clone method is available for just this purpose, but for custom classes you have written yourself you will need to implement the ICloneable interface (or just write your own clone method). For deep cloning, where you need to have all objects, including child objects, to be cloned rather than just assigning reference pointers to existing objects, this can be a little tricky. One method is to serialize the class into a memory stream and deserialize it to a new object, but the drawback here is that you can only clone classes that are binary-serializable. My classes are mostly XML serializable, so I wanted to use a different approach.&lt;br /&gt;&lt;br /&gt;A search on the web brought up various samples of code to do deep cloning using reflection and recursion, but it took a little work and debugging on my part to get this approach to work correctly.&lt;br /&gt;&lt;br /&gt;The first step was to create a base class that implements the ICloneable interface and uses reflection to clone the object, along with recursion for deep cloning child objects. I got a head start on this from a code sample I found perusing The Code Project (http://www.codeproject.com), so props go to Renfield78 at the code project for his post that got me started on this. Anyway, to make a long story short, with a little work and some debugging I was able to create base class in VB.NET that deep cloned object classes that inherit from it.&lt;br /&gt;&lt;br /&gt;This code worked fine until it ran into properties based on Dictionary and/or List classes. Since I use these two generic collection classes frequently, I wrote two classes that inherited from these that implemented the ICloneable interface as well, after which I was able to clone a fairly deep class-hiearchy successfully.&lt;br /&gt;&lt;br /&gt;The cloneable dictionary class (in simplified form) looks like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Reflection&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; CloneableDictionary(Of T)&lt;br /&gt;    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Dictionary(Of T)&lt;br /&gt;    &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Clone() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt; &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable.Clone&lt;br /&gt;        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; NewDictionary &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; CloneableDictionary(Of T)&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;.Count &amp;gt; 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ICloneType &lt;span class="kwrd"&gt;As&lt;/span&gt; Type = &lt;span class="kwrd"&gt;Me&lt;/span&gt;(0).&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetInterface(&lt;span class="str"&gt;"ICloneable"&lt;/span&gt;, &lt;span class="kwrd"&gt;True&lt;/span&gt;)&lt;br /&gt;            &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; (ICloneType &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; Value &lt;span class="kwrd"&gt;As&lt;/span&gt; T &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;&lt;br /&gt;                    NewDictionary.Add(&lt;span class="kwrd"&gt;CType&lt;/span&gt;(Value, ICloneable).Clone)&lt;br /&gt;                &lt;span class="kwrd"&gt;Next&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; MethodsList() &lt;span class="kwrd"&gt;As&lt;/span&gt; MethodInfo = &lt;span class="kwrd"&gt;Me&lt;/span&gt;(0).&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetMethods&lt;br /&gt;                &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; Value &lt;span class="kwrd"&gt;As&lt;/span&gt; T &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;&lt;br /&gt;                    NewDictionary.Add(Value)&lt;br /&gt;                &lt;span class="kwrd"&gt;Next&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Return&lt;/span&gt; NewDictionary&lt;br /&gt;        &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Return&lt;/span&gt; NewDictionary&lt;br /&gt;        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The cloneable list class is likewise pretty simple:&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Reflection&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; CloneableList(Of T)&lt;br /&gt;    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; List(Of T)&lt;br /&gt;    &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Clone() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt; &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable.Clone&lt;br /&gt;        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; NewList &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; CloneableList(Of T)&lt;br /&gt;        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;.Count &amp;gt; 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ICloneType &lt;span class="kwrd"&gt;As&lt;/span&gt; Type = &lt;span class="kwrd"&gt;Me&lt;/span&gt;(0).&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetInterface(&lt;span class="str"&gt;"ICloneable"&lt;/span&gt;, &lt;span class="kwrd"&gt;True&lt;/span&gt;)&lt;br /&gt;            &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; (ICloneType &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; Value &lt;span class="kwrd"&gt;As&lt;/span&gt; T &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;&lt;br /&gt;                    NewList.Add(&lt;span class="kwrd"&gt;CType&lt;/span&gt;(Value, ICloneable).Clone)&lt;br /&gt;                &lt;span class="kwrd"&gt;Next&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; MethodsList() &lt;span class="kwrd"&gt;As&lt;/span&gt; MethodInfo = &lt;span class="kwrd"&gt;Me&lt;/span&gt;(0).&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetMethods&lt;br /&gt;                &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; Value &lt;span class="kwrd"&gt;As&lt;/span&gt; T &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;&lt;br /&gt;                    NewList.Add(Value)&lt;br /&gt;                &lt;span class="kwrd"&gt;Next&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Return&lt;/span&gt; NewList&lt;br /&gt;        &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Return&lt;/span&gt; NewList&lt;br /&gt;        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;&lt;br /&gt;Hopefully the above should be pretty self-explanatory. So long as you inherit from or use these classes in place of the base classes and the collections contain objects that implement the ICloneable interface (as shown below), these can be used with a deep cloning base class.&lt;br /&gt;&lt;br /&gt;The base class for all your other object classes will then need to look something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Reflection&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;MustInherit&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; CloneableObject&lt;br /&gt;&lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Clone(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vObj &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt;)&lt;br /&gt;        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; vObj &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;If&lt;/span&gt; vObj.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.IsValueType &lt;span class="kwrd"&gt;OrElse&lt;/span&gt; vObj.&lt;span class="kwrd"&gt;GetType&lt;/span&gt; &lt;span class="kwrd"&gt;Is&lt;/span&gt; Type.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;(&lt;span class="str"&gt;"System.String"&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;Return&lt;/span&gt; vObj&lt;br /&gt;            &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; newObject &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt; = Activator.CreateInstance(vObj.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;)&lt;br /&gt;                &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; newObject.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetInterface(&lt;span class="str"&gt;"IEnumerable"&lt;/span&gt;, &lt;span class="kwrd"&gt;True&lt;/span&gt;) &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;AndAlso&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; newObject.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetInterface(&lt;span class="str"&gt;"ICloneable"&lt;/span&gt;, &lt;span class="kwrd"&gt;True&lt;/span&gt;) &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                    &lt;span class="rem"&gt;'This is a cloneable enumeration object so just clone it&lt;/span&gt;&lt;br /&gt;                    newObject = &lt;span class="kwrd"&gt;CType&lt;/span&gt;(vObj, ICloneable).Clone&lt;br /&gt;                    &lt;span class="kwrd"&gt;Return&lt;/span&gt; newObject&lt;br /&gt;                &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;                    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; Item &lt;span class="kwrd"&gt;As&lt;/span&gt; PropertyInfo &lt;span class="kwrd"&gt;In&lt;/span&gt; newObject.&lt;span class="kwrd"&gt;GetType&lt;/span&gt;.GetProperties&lt;br /&gt;                        &lt;span class="rem"&gt;'If a property has the ICloneable interface, then call the interface clone method&lt;/span&gt;&lt;br /&gt;                        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; (Item.PropertyType.GetInterface(&lt;span class="str"&gt;"ICloneable"&lt;/span&gt;) &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                            &lt;span class="kwrd"&gt;If&lt;/span&gt; Item.CanWrite &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; IClone &lt;span class="kwrd"&gt;As&lt;/span&gt; ICloneable = &lt;span class="kwrd"&gt;CType&lt;/span&gt;(Item.GetValue(vObj, &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;), ICloneable)&lt;br /&gt;                                Item.SetValue(newObject, IClone.Clone, &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;)&lt;br /&gt;                            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;                        &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;                            &lt;span class="rem"&gt;'Otherwise just set the value&lt;/span&gt;&lt;br /&gt;                            &lt;span class="kwrd"&gt;If&lt;/span&gt; Item.CanWrite &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br /&gt;                                Item.SetValue(newObject, Clone(Item.GetValue(vObj, &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;)), &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;)&lt;br /&gt;                            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;                        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;                    &lt;span class="kwrd"&gt;Next&lt;/span&gt;&lt;br /&gt;                    &lt;span class="kwrd"&gt;Return&lt;/span&gt; newObject&lt;br /&gt;                &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Clone() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt; &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.ICloneable.Clone&lt;br /&gt;        &lt;span class="kwrd"&gt;Return&lt;/span&gt; Clone(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)&lt;br /&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Assuming all of your custom classes derive from the above and all properties that are based on dictionary or list objects use the appropriate ICloneable implementation (as show in the first two code samples), these three classes in conjunction should provide most of your needs in being able to deep clone your own custom classes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/38481552-116787475867519568?l=programmingcorner.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmingcorner.blogspot.com/feeds/116787475867519568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=38481552&amp;postID=116787475867519568' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/116787475867519568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/38481552/posts/default/116787475867519568'/><link rel='alternate' type='text/html' href='http://programmingcorner.blogspot.com/2007/01/deep-cloning-in-net.html' title='Deep Copy - Cloning in VB .NET'/><author><name>Nathan Cartwright</name><uri>https://profiles.google.com/107076442995264361782</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh6.googleusercontent.com/-_AXJ_WiFDKs/AAAAAAAAAAI/AAAAAAAAAGk/yfcJmO9nkZk/s512-c/photo.jpg'/></author><thr:total>11</thr:total></entry></feed>
