Mono源代码学习笔记:Console类(一)
前言 我们知道,Mono 是 .NET Framework 跨平台的开源实现。Mono 的源代码就是金矿,等待我们去挖掘。 目前 Mono 的最新版本是 Mono 2.8.2,可以到 下载 mono-2.8.2.tar.bz2,文件大小是30MB。可以参阅在 Ubuntu 10.10 操作系统安装 Mono 2.8.2一文。 现在,
前言
我们知道,Mono 是 .NET Framework 跨平台的开源实现。Mono 的源代码就是金矿,等待我们去挖掘。
目前 Mono 的最新版本是 Mono 2.8.2,可以到 下载 mono-2.8.2.tar.bz2,文件大小是30MB。可以参阅“在 Ubuntu 10.10 操作系统安装 Mono 2.8.2”一文。
现在,让我们来看看 Mono 是如何实现 .NET Framework Base Class Library 中的 System.Console 类的。Console 类在 CUI 应用程序中有着十分重要的作用。而且,从 .NET Framework 2.0 开始,Console 类除了实现基本的控制台输入/输出功能以外,还实现了诸如更改前景色和背景色等高级功能。下图就是一个例子:
Microsoft 实现的 Console 类只需要考虑 Windows 操作系统的控制台就行了,但是 Mono 的 Console 类就必须考虑跨平台了,要能够工作在 Windows 和 Unix 操作系统中。所以是比较复杂的。
准备在自己的工作目录下编译出 Console.dll 程序集为了研究 Console 类的源代码,找出和 Console 类密切相关的源代码,,我准备从 Console.cs 出发在自己的工作目录下编译出一个 Console.dll 程序集。我们来看看 Console 类的源代码位于 Mono 体系的什么位置:
ben@ben-1520:~$ cd src/mono-2.8.2 ben@ben-1520:~/src/mono-2.8.2$ find . -name Console.cs ./mcs/class/corlib/System/Console.cs ben@ben-1520:~/src/mono-2.8.2$哦,因为 Console 类定义于 .NET Framework 最核心的 mscorlib.dll 程序集,并且位于 System 命名空间。所以上面的命令就显示这样的结果了。
我们现在准备一个工作目录:
ben@ben-1520:~$ cd ~/work ben@ben-1520:~/work$ mkdir Console ben@ben-1520:~/work$ cd Console ben@ben-1520:~/work/Console$先把上述 Console.cs 从 Mono 的源代码目录拷贝到工作目录中,然后使用 C# 编译器进行编译,根据出错信息来决定还要拷贝哪些文件。最终,发现需要 Mono 源代码 mcs/build/common、mcs/class/corlib/System 和 mcs/class/corlib/System.IO 目录下的 23 个 C# 源程序文件,在上面三个目录分别是 2 个、18 个和 3 个。我编写了一个如下内容的 mksrc.sh 脚本,用于将 Mono 2.8.2 的源代码与 Console 类相关的源文件复制到工作目录中:
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/System 17: rm ConsoleColor.cs ConsoleKey.cs ConsoleModifiers.cs ConsoleSpecialKey.cs 18: cd ~/work/Console 从 Stream.cs 中分离出 NullStream 类的源代码在 Mono 的源代码的 Stream.cs 文件中,包含以下三个类:
因为我们需要用到 NullStream 类,所以将工作目录中 mcs/class/corlib/System.IO/ 目录下的 Stream.cs 改名为 NullStream.cs,然后在该文件中删除另外两个类的源代码,只保留 NullStream 类的源代码。
虽然 Mono 是一项伟大的开源工程,大师们写的代码也非常精彩。但是在这里还是有点疏忽了。如果 Mono 的源代码的 Stream.cs 文件中只包含 Stream 类,而把 NullStream 类的源代码放在 NullStream.cs 文件中,把 SynchronizedStream 类的源代码放在 SynchronziedStream.cs 文件中。我们也就可以直接使用 NullStream.cs 文件了,不用象现在一样又是改名,又是删除代码。
编写辅助代码现在让我们编写一些简单的辅助代码,以便能够编译生成 Console.dll 程序集文件。
ben@ben-1520:~/work/Console$ mkdir Skyiv ben@ben-1520:~/work/Console$ cd Skyiv ben@ben-1520:~/work/Console/Skyiv$ gedit AssemblyInfo.cs Environment.cs ExtensionMethods.cs MonoIO.cs Stub.cs ben@ben-1520:~/work/Console/Skyiv$下面就是 AssemblyInfo.cs,仅仅是简单地使用 CLSCompliantAttribute 标记程序集,指示该程序集符合公共语言规范。
1: using System; 2: 3: [assembly:CLSCompliant(true)]下面就是 Environment.cs,主要是因为在 Console.cs、ConsoleDrivers.cs 这两个源程序中使用了 Environment 静态类的 IsRunningOnWindows 静态属性,而这个属性是 internal 的。所以我们必须自己提供一个,不然程序无法通过编译。我们还必须提供在其他源程序中有使用的 NewLine 属性以及 Exit 和 GetEnvironmentVariable 方法。这三个成员原来是 public 的,但是我们自己用的话,使用 internal 来修饰是没有问题的。注意,IsRunningOnWindows 属性指示是否运行在 Windows 操作系统中,这在 Microsoft 的 .NET Framework 中是没有的,因为 Microsoft 的实现总是运行在 Windows 操作系统中。Mono 为了跨平台才需要这个属性。
01: namespace System 02: { 03: static class Environment 04: { 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: }
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

Nintendo has opened pre-orders for the latest version of the Switch Lite (curr. $189.99 on Amazon). However, the device is not available to order globally just yet. To recap, the company presented the Switch Lite Hyrule Edition almost two weeks ago d

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:
