Mono source code learning notes: Console class (1)

Source: Internet
Author: User
Preface we know that Mono is an open-source implementation of. NETFramework cross-platform. The source code of Mono is Gold Mine, waiting for us to explore. Mono-2.8.2.tar.bz2 can be downloaded from mono2.8.2.8.2.tar.bz2. The file size is 30 MB. For more information, see installing Mono2.8.2 on Ubuntu10.10. Now,

We know that Mono is an open-source implementation of. NET Framework across platforms. The source code of Mono is Gold Mine, waiting for us to explore. Currently the latest version of Mono is Mono 2.8.2, you can download the mono-2.8.2.tar.bz2, the file size is 30 MB. For more information, see install Mono 2.8.2 on Ubuntu 10.10. Now,

Preface

We know that Mono is a cross-platform open-source implementation of. NET Framework. The source code of Mono is Gold Mine, waiting for us to explore.

Currently the latest version of Mono is Mono 2.8.2, you can download the mono-2.8.2.tar.bz2, the file size is 30 MB. See the article "installing Mono 2.8.2 in Ubuntu 10.10.

Now let's take a look at how Mono implements the System. Console Class in the. NET Framework Base Class Library. The Console class plays an important role in CUI applications. In addition to basic Console input/output functions, the Console class also implements advanced functions such as changing the foreground color and background color from. NET Framework 2.0. Is an example:

The Console class implemented by Microsoft only needs to consider the Console of the Windows operating system. However, the Console class of Mono must consider cross-platform and work in Windows and Unix operating systems. So it is complicated.

Prepare to compile the Console. dll assembly in your working directory.

To study the source code of the Console class and find out the source code closely related to the Console class, I am going to compile a Console. dll assembly under my working directory from Console. cs. Let's take a look at the location of the source code of the Console class in the Mono system:

Ben @ ben-1520 :~ $ Cd src/mono-2.8.2ben @ ben-1520 :~ /Src/mono-2.8.2 $ find.-name Console. cs./mcs/class/corlib/System/Console. csben @ ben-1520 :~ /Src/mono-2.8.2 $

Oh, because the Console class is defined in the mscorlib. dll assembly at the core of. NET Framework and is located in the System namespace. Therefore, the above command shows the result.

Now we prepare a working directory:

Ben @ ben-1520 :~ $ Cd ~ /Workben @ ben- 1520 :~ /Work $ mkdir Consoleben @ ben-1520 :~ /Work $ cd Consoleben @ ben- 1520 :~ /Work/Console $

First, copy the above Console. cs from the Mono source code directory to the working directory, then use the C # compiler for compilation, and decide which files to copy based on the error information. Finally, it is found that the Mono source code mcs/build/common, mcs/class/corlib/System and mcs/class/corlib/System are required. 23 C # source code files under the IO directory. The three directories above are 2, 18, and 3 respectively. I have compiled the following mksrc. sh script to copy the source code of Mono 2.8.2 to the working directory related to the Console class:

01: cd ~ /Work/Console02: rm-rf mcs03: mkdir mcs04: mkdir mcs/build05: mkdir mcs/build/common06: mkdir mcs/class07: mkdir mcs/class/corlib08: mkdir mcs/class/corlib/System09: mkdir mcs/class/corlib/System. IO10: cd ~ /Src/mono-2.8.2/mcs/build/common11: cp Locale. cs MonoTODOAttribute. cs ~ /Work/Console/mcs/build/common12: cd ~ /Src/mono-2.8.2/mcs/class/corlib/System. IO13: cp Stream. cs UnexceptionalStream * er. cs ~ /Work/Console/mcs/class/corlib/System. IO14: cd ~ /Src/mono-2.8.2/mcs/class/corlib/System15: cp Buffer. cs Control *. cs CStream *. cs * Term *. cs * Console *. cs ~ /Work/Console/mcs/class/corlib/System16: cd ~ /Work/Console/mcs/class/corlib/System17: rm ConsoleColor. cs lelekey. cs ConsoleModifiers. cs lelespecialkey. cs18: cd ~ /Work/Console extract the NullStream class source code from Stream. cs

The Stream. cs file of Mono Source Code contains the following three classes:

Because we need to use the NullStream class, the mcs/class/corlib/System. stream in the IO/directory. cs is renamed as NullStream. cs, and then delete the source code of the other two classes in the file, only keep the source code of the NullStream class.

Although Mono is a great open-source project, the code written by masters is also wonderful. However, it is still a bit negligent here. If the Stream. cs file of the Mono source code only contains the Stream class, put the source code of the NullStream class in the NullStream. cs file, and put the source code of the SynchronizedStream class in the SynchronziedStream. cs file. We can also directly use the NullStream. cs file. We don't need to change the name or delete the code as we do now.

Compile auxiliary code

Now let's write some simple auxiliary code to compile and generate the Console. dll Assembly file.

Ben @ ben-1520 :~ /Work/Console $ mkdir Skyivben @ ben- 1520 :~ /Work/Console $ cd Skyivben @ ben- 1520 :~ /Work/Console/Skyiv $ gedit AssemblyInfo. cs Environment. cs ExtensionMethods. cs MonoIO. cs Stub. csben @ ben-1520 :~ /Work/Console/Skyiv $

The following is AssemblyInfo. cs. It simply uses CLSCompliantAttribute to mark the Assembly and instructs the Assembly to comply with common language specifications.

1: using System; 2: 3: [assembly: CLSCompliant (true)]

The following is the Environment. cs, mainly because the IsRunningOnWindows static attribute of the Environment static class is used in the Console. cs and ConsoleDrivers. cs source programs, and this attribute is internal. Therefore, we must provide one by ourselves. Otherwise, the program cannot be compiled. We must also provide the NewLine attribute used in other source programs as well as the Exit and GetEnvironmentVariable methods. The three members are originally public, but we can use internal to modify them. Note that the IsRunningOnWindows attribute indicates whether to run in a Windows operating system, which is not in Microsoft's. NET Framework, because Microsoft's implementation always runs in a Windows operating system. Mono requires this attribute for cross-platform purposes.

01: namespace System02: {03: static class Environment04: {05: internal static bool IsRunningOnWindows {get {return false ;}}}07: internal static void Exit (int code) {} 08: internal static string GetEnvironmentVariable (string value) {return value;} 09:} 10 :}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.