Mono source code learning notes: Console class (1)

Source: Internet
Author: User
Preface

We know that Mono is a cross-platform open-source implementation of. NET Framework. Mono'sSource codeIt is a gold mine, waiting for us to explore.

Currently the latest version of Mono is mono 2.8.2, you can go to the http://ftp.novell.com/pub/mono/sources/mono/ to 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. Console class in cui ApplicationProgramHas a very important role. 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 of the console classCodeTo find out the source code that is closely related to the console class. I am going to compile a console. dll assembly in 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/console 02: Rm-RF MCS 03: Mkdir MCS 04: Mkdir MCS/build 05: Mkdir MCS/build/common 06: Mkdir MCS/class 07: Mkdir MCS/class/corlib 08: Mkdir MCS/class/corlib/System 09: Mkdir MCS/class/corlib/system. Io 10: Cd ~ /Src/mono-2.8.2/MCS/build/common 11: CP locale. CS monotodoattribute. Cs ~ /Work/console/MCS/build/common 12: Cd ~ /Src/mono-2.8.2/MCS/class/corlib/system. Io 13: CP stream. CS unexceptionalstream * er. Cs ~ /Work/console/MCS/class/corlib/system. Io 14: Cd ~ /Src/mono-2.8.2/MCS/class/corlib/System 15: CP buffer. CS control *. CS cstream *. CS * Term *. CS * console *. Cs ~ /Work/console/MCS/class/corlib/System 16: Cd ~ /Work/console/MCS/class/corlib/System17: Rm consolecolor. CS consolekey. CS consolemodifiers. CS lelespecialkey. CS 18: Cd ~ /Work/console
Extract nullstream class source code from stream. CS

The stream. CS file of Mono Source Code contains the following three classes:

    1. Public abstract classStream: Delealbyrefobject, idisposable
    2. Internal classNullstream: Stream
    3. Internal classSynchronizedstream: Stream

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:UsingSystem;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 ;}} 06:      Internal static string Newline { Get { Return "\ N" ;}} 07:      Internal static void Exit ( Int Code ){} 08:      Internal static string Getenvironmentvariable ( String Value ){ Return Value ;} 09: } 10: }

The following is extensionmethdos. cs. It only provides one extension method, that is, the dataavailable method used in the streamreader class. In the streamreader class of Mono, The dataavailable method is internal, so we must provide one by ourselves. This dataavailable method is used in the terminfodriver. Cs source program.

 01:  Namespace System. Io 02: { 03:    Static class  Extensionmethods04: { 05:      Internal static bool Dataavailable ( This Streamreader Reader) 06: { 07:        Return Reader. Peek ()! =-1; 08: } 09: } 10: }

The following is monoio. CS. This monoio class is used in the console. CS and consoledriver. Cs source programs. It is strange that if this monoio class is not provided, it can be compiled successfully in the mono 2.8.2 environment of ubuntu 10.10, but in the Windows operating system.. NET Framework 4 cannot be compiled successfully.

 1:  Namespace System. Io2: { 3:    Static class  Monoio4: { 5:      Internal static  Intptr Consoleinput { Get { Return ( Intptr ) 0 ;}} 6:      Internal static  Intptr Consoleoutput { Get { Return ( Intptr ) 1 ;}} 7:      Internal static  Intptr Consoleerror { Get { Return ( Intptr ) 2 ;}} 8: } 9: }

Below is stub. CS, which provides some internal static members of the encoding and textwriter classes, as well as the internal constructor of the filestream class, and an implicit conversion operator to take our skyiv. stub. filestream is implicitly converted to system. io. filestream. These things are only used in the console. Cs source program.

 01:  Using System; 02:  Using System. IO; 03:  04:  Namespace Skyiv. Stub 05: { 06:    Static class Encoding07: { 08:      Internal static System. Text. Encoding Utf8unmarked { Get { Return System. Text. Encoding . Utf8 ;}} 09:      Internal static void Internalcodepage ( Ref int Code_page ){} 10: }11:    12:    Static class  Textwriter13: { 14:      Internal static System. Io. Textwriter Synchronized (system. Io. Textwriter Writer, Bool Neverclose) 15: { 16:        Return Writer; 17: } 18: } 19:    20:    Sealed class  Filestream21: { 22:          Internal Filestream ( Intptr Handle, Fileaccess Access, Bool Ownshandle,Int Buffersize, Bool Isasync, Bool Iszerosize) 23: { 24: } 25:          26:          Public Static Implicit Operator System. Io. Filestream ( Filestream Value) 27: {28:            Return null ; 29: } 30: } 31: }
Modify the console. Cs source code

Now, modify console. the Cs source program adds "skyiv. stub. to call our own version.

Generate and compile the compilation response File
 
Ben @ Ben-1520 :~ /Work/console/skyiv $CD ..Ben @ Ben-1520 :~ /Work/console $Ls skyiv/*. cs mcs/build/*. cs mcs/class/*. Cs> make. RSPBen @ Ben-1520 :~ /Work/console $Gedit make. RSPBen @ Ben-1520 :~ /Work/console $DMCS @ Make. RSPBen @ Ben-1520 :~ /Work/console $

When you use gedit to edit make. RSP, add the following four lines:

 01: -T: Library 02: -Out: console. dll 03: -Unsafe 04: -Nowarn: 436 05: MCS/build/common/locale. CS 06: MCS/build/common/monotodoattribute. CS 07: MCS/class/corlib/system/buffer. CS 08: MCS/class/corlib/system/lelecanceleventargs. CS09: MCS/class/corlib/system/lelecanceleventhandler. CS 10: MCS/class/corlib/system/console. CS 11: MCS/class/corlib/system/consoledriver. CS 12: MCS/class/corlib/system/consolekeyinfo. CS 13: MCS/class/corlib/system/controlcharacters. CS 14: MCS/class/corlib/system/cstreamreader. CS 15: MCS/class/corlib/system/cstreamwriter. CS 16: MCS/class/corlib/system/iconsoledriver. CS 17: MCS/class/corlib/system. IO/nullstream. CS 18: MCS/class/corlib/system. IO/unexceptionalstreamreader. CS 19: MCS/class/corlib/system. IO/unexceptionalstreamwriter. CS 20: MCS/class/corlib/system/knownterminals. CS 21: MCS/class/corlib/system/nullconsoledriver. CS 22: MCS/class/corlib/system/terminfobooleans. CS 23: MCS/class/corlib/system/terminfodriver. CS24: MCS/class/corlib/system/terminfonumbers. CS 25: MCS/class/corlib/system/terminforeader. CS 26: MCS/class/corlib/system/terminfostrings. CS 27: MCS/class/corlib/system/windowsconsoledriver. CS 28: Skyiv/assemblyinfo. CS 29: Skyiv/environment. CS 30: Skyiv/extensionmethods. CS 31: Skyiv/monoio. CS32: Skyiv/stub. CS

Finally, we successfully compiled the console. dll Assembly file using the C # compiler. Of course, this console. dll is only used to study the mono source code and cannot work normally, because we use some self-compiled auxiliary code to replace the mono system.

View the directory structure of the source program

The following is the contents of our working directory:

Relationships between various types

In, the core types are as follows:

    1. Console: Public static class. Call the static methods and static attributes of the consoledriver class to work.
    2. Consoledriver: Internal class, which contains an internal static field of the iconsoledriver interface.
    3. Iconsoledriver: Internal interface. The following three classes all implement the iconsoledriver interface.
    4. Nullconsoledriver: Internal class, which only provides the basic console input/output functions and is used for dumb terminals.
    5. Terminfodriver: Internal class, used for various terminals of UNIX operating systems.
    6. Windowsconsoledriver: Internal class, used in the Windows operating system console.

You can click to download console.7z.

 

(To be continued)

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.