Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


An Introduction to Mono Development : Page 4

Microsoft's .NET Framework CLI implementation isn't the only CLI development game in town. The Mono project is both surprisingly mature, and surprisingly compatible—and you can deliver applications built on it to multiple platforms, including Windows.




Application Security Testing: An Integral Part of DevOps

Building a Console Application in Mono
Your first Mono client will be a simple console based application named ConsoleClientApp.exe. Create a new C# file named ConsoleClientApp.cs that contains the following code:

// This client app makes use of the CoreLibDumber.DLL // to dump types to a file. using System; using CoreLibDumper; namespace ConsoleClientApp { public class Program { public static void Main() { Console.WriteLine( "***** The Type Dumper App *****\n"); // Ask user for name of type. string typeName = ""; Console.Write("Please enter type name: "); typeName = Console.ReadLine(); // Now send it to the helper library. if(TypeDumper.DumpTypeToFile(typeName)) Console.WriteLine("Data saved into {0}.txt", typeName); else Console.WriteLine( "Error! Can't find that type..."); } } }

Notice that the Main() method in the preceding code simply prompts the user for a fully-qualified type name. The TypeDumper.DumpTypeToFile() method uses the user-entered name to dump the type's members to a local file. Next, create a *.rsp file that references CoreLibDumper.dll:

/target:exe /out:ConsoleClientApp.exe /reference:CoreLibDumper.dll ConsoleClientApp.cs

Finally, compile the executable using gmcs.exe as shown here:

gmcs @ConsoleClientApp.rsp

Before you run this application under the Mono runtime, take a moment to rename the CoreLibDumper.dll assembly to DontUseCoreLibDumper.dll using Windows Explorer. The reason is that you've referenced a strongly-named assembly, and therefore wish to use the copy of CoreLibDumper.dll that resides in the GAC, not the local copy in the client's application directory.

Figure 5. Sample Application Output: Enter "System.Threading.Thread" at the sample application's prompt, and you'll find the Thread class members in the System.Threading.Thread.txt file.
With this detail out of the way, you can load ConsoleClientApp.exe into the Mono runtime engine by specifying the name of the executable (including the .exe file extension) as an argument to mono.exe:

mono ConsoleClientApp.exe

For testing purposes, enter System.Threading.Thread at the prompt, and press the Enter key. The program will process the type, and create new file named System.Threading.Thread.txt containing the type's metadata definition. Figure 5 shows this file opened in NotePad.

Before building a Windows Forms client, try the following experiment. Open the Windows Explorer and attempt to run your client application by double clicking on ConsoleClientApp.exe. You might be surprised to find that doing so causes a FileNotFoundException.

At first glance you might assume this is due to the fact that you renamed CoreLibDumper.dll to DontUseCoreLibDumper.dll. However the true reason is because you just loaded ConsoleClientApp.exe into the Microsoft CLR!

To run an application under Mono, you must pass it into the Mono runtime via mono.exe. If you do not, you will be loading your into the Microsoft CLR, which assumes all shared assemblies are installed into the Microsoft GAC located in the <%windir%>\Assembly directory. In fact, that wouldwork—but then you could only use the assembly from .NET applications.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date