- Code Distribution: An Old Problem
- Creating a C# Assembly
- Using an Assembly in a C# Client Program
- Adding to the Assembly Code
- Conclusion
Using an Assembly in a C# Client Program
The method of code distribution in assemblies follows a very old modelit's the model of shared libraries. To use the assembly code, we need to create a client, and we can do this using Microsoft Visual C# 2008 Express Edition. Close the class library solution, select File > New Project, choose Console Application as the project type, enter a name (if you want a non-default name, such as ConsoleApplicationUsingAssembly), and then click OK.
I'm pretty impressed with Microsoft Visual C# 2008 Express Edition. It's obvious from using this and similar Microsoft tools that a great deal of effort has been put into streamlining the tool-specific development process. This is a very good investment on the part of Microsoft, because it reduces the learning curve, and overall it saves development time. In turn, this helps make the .NET platform more competitive in the broader industry.
Anyway, we now have our client application, from which we will use the class library code. To do this, make the changes illustrated in Listing 2or just select all the code in the editor and paste in the contents of Listing 2.
Listing 2 The client code using the class library.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ClassLibrary1; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Class1 myClass1 = new Class1(); myClass1.dumpPlatformDetails(); System.Console.ReadLine(); } } }
Not too many surprises in Listing 2. I just instantiate an object of the class called Class1, which is clearly from the class library we created earlier. Once the object is created, I call the method myClass1.dumpPlatformDetails(). Then, to stop the program from exiting before you get a chance to look at the output, I call System.Console.ReadLine(). To exit the program, just press any key when the DOS console has focus.
Listing 3 shows the program output from Listing 2.
Listing 3 The program output - at last!
HasShutdownStarted: False MachineName: MYPC OSVersion: Microsoft Windows NT 5.1.2600 Service Pack 3 TickCount: 9795812 UserDomainName: MYPC UserInteractive: True UserName: Stephen OS Version: 2.0.50727.3082 WorkingSet: 17604608 Logical Drives: C:\, D:\, E:\
In Listing 3, the output value of HasShutdownStarted is False. This parameter indicates that the host machine is not shutting down. Notice the value of the OSVersion parameter, which is Microsoft Windows NT 5.1.2600 Service Pack 3. The machine on which I ran the program is actually running Windows XP, so the value of OSVersion illustrates the lineage of the Windows XP operating system, which is based on Windows NT technology. The parameter TickCount indicates how long (in milliseconds) the machine has been running; here, the value of TickCount is equivalent to about 2.72 hours.
What type of application might be interested in details such as those in Listing 3? Aside from general curiosity, IT management applications typically consume details such as these. For example, the value of the TickCount parameter might be sampled every day and added to a counter. When the aggregate value reaches a certain threshold, it might be time to replace the machine. Likewise, this type of output can be useful in determining the software on a given machine.