Effective C #: 1. Correct Area Classification Set, Module and Name Space

zhaozj2021-02-17  69

Effective C #: 1. Correct Area Classification Set, Module and Name Space

Chen Ming Microsoft C # /. Net Asia MVP

Difficult: 4/10

.NET is a development platform for network applications and services, let us start from a simple web application - although this is not the topic to be discussed in this chapter.

//Hello.cs: say hello to the internet

Using system;

Using system.net;

Namespace effective.csharp.chapter1 {

Public class hellointernet {

Public static void main () {

String address = "http://www.google.com/search?q=";

String hello = "Hello, Internet";

WebRequest MyReq = WebRequest.create (Address Hello);

WebResponse myresponse = myreq.getResponse ();

MyResponse.close ();

}

}

}

Before you ask the Internet, let's compile our procedure because the program is referenced by the system.net, so the compile command seems to be like this:

CSC /R :system.net.dll Hello.cs

But the compiler quickly complained:

Error CS0006: Metadata file 'system.net.dll' Could Not Be Found

It seems that the compiler does not find the system.net.dll file, but how will it? The library reference is clearly written in WebRequest and WebResponse is in the System.net namespace! Before solving this problem, let's take a look at the definition of the following concept:

Namespace: C and Java programmers should be unfamiliar with the concept of namespace, namespaces are used to organize a set of functions related to a set of functions to avoid conflicts caused by simple type names. For example, a category library that may contain "tree" in a biological class library, and the "tree" structure needs to be defined in the application of the computer data structure. In a system without a namespace concept, due to a named conflict, we will not use both types of types in the same application. And in .NET to use the namespace to solve this problem is easy to mention:

Namespace biology {

Public class tree {

// Biology to "tree"

}

}

Namespace Datastructure {

Public class tree {

/ / Description of "Tree" in the computer data structure

}

}

In this way, as long as the "namespace. Type Name" in the program reference type, you can completely avoid ambiguity:

// example of biology "tree"

Biology.tree Tree1 = New Biology.tree ("PINE");

// "Tree" instance of the data structure

DataStructure.Tree Tree2 = New DataStructure.tree ();

The reason why the namespace is just "logically" organization type because it only sends different types of different types only in the design development phase. The running system does not pay attention to the existence of the namespace, but simply looks a part of the namespace as a part of the name. There is no directory or file corresponding to the namespace, and it is not necessarily stored in the same name space.

C # Allows to simplify the reference in the namespace using the USING statement, which is functionally closer to the C Using Namespace statement, not the Java's import. It only provides a simple form for programmers in the code, rather than providing the actual storage location of any type of information for the compiler, such as: System.Console.writeline ("Hello");

Completely equivalent to no name conflict:

Using system;

...

Console.writeline ("Hello");

Even if there is a naming conflict, using the USING statement can also greatly simplify the recruitment of lengthy namespaces. For example, there is a class named Button in two namespaces in System.Windows.Forms and System.Web.ui.Controls. If we must use these two classes at the same time (in fact usually Less will quote these two classes at the same time, here is only the problem), you can write this:

Using Winform = System.Windows.Forms;

Using Webform = System.Web.ui.Controls;

Winform.Button WinBTN = New Winform.Button ();

Webform.Button Webbtn = New WebForm.Button ();

So since the namespace cannot provide the location of the actual type information, how does the compiler get these types of information to complete compilation? How is .NET how to physically organize various types? To this end, .NET introduces a class set (Assembly) concept.

ASSEMBLY: The class set is the relevant type of physical organization in the .NET. It is the basic unit for application deployment, version control, reuse, and permission assignment. The class set contains a class list list (MANIFEST), the type information defined in the class set, and the implementation code of the class method, and other resources. The class set can be composed of one or more files, or contain resource data, or contain type information and implementation PE format files.

The PE format file containing type information and implementation is called a module, and the module is the basic unit loaded by the runtime. If the program references a class in the module, the CLR will load the entire module into memory. For more information on how to effectively divide the modules to improve program performance, please refer to Terms X.

The class set list contains information metadata at the class set level, such as the name, version of the class set, and the files it contain. The class list must be completely saved in a module of the class set, as shown below:

When compiling C # programs, the compiler requires to indicate which class sets are found to find the type of current program reference, for example: System.Console classes are included in the Mscorlib class, then compile a compile command that references System.Console. for:

CSC /R: Mscorlib.dll myprog.cs

Among them, / r: referenced Mscorlib.dll is a module that contains class sets in the MScorlib class. (In fact, I don't need special references for Mscorlib class, will be introduced later)

Class set and namespace relationship

The description of the relationship between the .NET document is: "Namespace is conceptual and integrated with class integration". With more popular words, there is no direct contact between them. Classs in a namespace may be distributed in several class sets, and a class set may also contain several different namespaces. There is indeed a lot of class sets and namespaces in the system class library, but it can only be said that it is a coincidence.

The following table lists the corresponding relationships between common namespaces and their implementation class:

Name space

Corresponding class set System

Mscorlib, System

SYSTEM.IO

Mscorlib, System

SYSTEM.XML

System.Data, System.xml

System.data

System.data

SYSTEM.NET

SYSTEM

System.Reflection

Mscorlib,

System.security

Mscorlib,

System.InteropServices

Mscorlib,

System.Runtime.Remoting

Mscorlib,

System.Runtime.Serialization

Mscorlib,

Here we see the implementation of the System.NET name space is included in the System class, so we must correctly compile the programs starting with this chapter. We must quote the system class set in the compile command instead of system.net:

CSC /R :system.dll Hello.cs

The program was successfully compiled.

Tip: In the .NET Framework document, we can easily find the namespace, class set of the specific type belonging. For example, an overview section of the document Console class contains the following information:

Public Sealed Class Console

...

Requirements

Namespace: System ß Type belongs to namespace

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server Family

Assembly: Mscorlib (in mscorlib.dll) ß

It seems that the story should be a paragraph, but - a moment: Since we must point to the compiler to specify the class set required in the program, do not compile a program that references the System.Console class, do not need to add / R : mscorlib.dll? Why is sometimes the type of System.xml namespace does not need to add /r :system.data.dll or /r :system.xml.dll?

There are two different situations here: Mscorlib class sets in the .NET class library is very special, all built-in types are defined in this class, and the root type Sytem.Object of the .NET class inheritance system is also defined here. The class is set. Almost all applications must reference this class set, so the C # compiler will automatically add reference to the MScorlib class set during the compilation process.

In addition, the C # compiler allows the use of the configuration file to simplify the settings of the compilation parameters, such as saving the following to the C # profile Myexe.RSP:

# 注释: Compile Source1.cs Source2.cs to generate myexe.exe

/ Target: exe /out:myexe.exe Source1.cs Source2.cs

Then you can compile the Myexe program by developing a configuration file:

CSC @ myexe.RSP

The class set that needs to be reference is also one of the contents of the configuration file, and the program needs to be referenced in the configuration file. In view of some types of sets of categories that are commonly used during the writing program, the designer of the C # places these class sets in the default configuration file of the C # compiler, the configuration file is located in% systemroot% / Microsoft.Net / Framework / % Version% / CSC.RSP, where% systemroot% is the installation directory of Windows, and% Version% is the version number of .NET Framework. The content of this file is as follows:

# This file contains command-line option the the c ## Command Line Compiler (CSC) Will Process As Part

# of every compilation, unless the "/ noconfig" option

# IS specified.

# Reference The Common Framework Libraries

/R :accessibility.dll

/r:MICROSoft.vsa.dll

/R :system.configuration.install.dll

/R :system.data.dll

/R :system.design.dll

/R :system.directoryServices.dll

/R :system.dll

/r :system.drawing.design.dll

/r :system.drawing.dll

/R :System.EnterpriseServices.dll

/r :system.management.dll

/r :system.messaging.dll

/r :system.runtime.remoting.dll

/r :System.Runtime.Serialization.Formatters.soap.dll

/R :system.security.dll

/r :system.serviceProcess.dll

/R :system.web.dll

/R :System.web.regularExpressions.dll

/r :system.web.services.dll

/R :system.windows.forms.dll

/R :system.xml.dll

In this way, the C # compiler will automatically reference all class sets listed above - unless the configuration file is ignored by using / noconfig explicitly specifies. (End) * This article is made of original works, please do not reprint without the author license.

转载请注明原文地址:https://www.9cbs.com/read-31359.html

New Post(0)