Wrox's C # Advanced Programming Third Edition Part 1 Chapter 1 (13 ~ 15)

xiaoxiao2021-03-06  125

Public language norm

Common Language Specification (CLS) is accommodated with CTS to ensure language interoperability. CLS is a minimum standard collection, all compilers that run the platform as the target of the target. Because IL is a rich language, many compilers have the authors like to implement a subset of the functional restrictions provided by IL and CTS. This way, as long as the compiler supports all the definitions supported by the CLS.

Tip: You can write code that is incompatible with CLS. However, if you do this, the compiled IL code cannot guarantee that the language interoperability is fully met.

For example, let's take a look at an example of writing about letters. IL is case sensitive. Developer developers in case sensitive developers using this case-sensitive flexibility to name them. However, Visual Basic .NET is not case sensitive. The work of CLS is to make CLS compatible code understand that they can't use any two names that are only different in case. In this way, Visual Basic .NET can work with any CLS compatible code.

This example shows that CLS has two ways of working. First, each compiler function does not have to be strong enough to meet all the characteristics of .NET, which encourages other compiler vendors to join their language to this big family. Second, he also provides such a guarantee: if you limit your class to meet the compatibility of CLS, then other compatible languages ​​written code can use your class.

The advantage of this method is that the limitations using CLS compatibility features are only applicable to public or protected members of public categories and classes. For the implementation of private members in your class, you can write any non-CLS compatible code because the code of other assemblies is not accessible anyway.

Here we don't need detailed discussion of the details of the CLS specification. In general, since the non-CLS compatibility characteristics in C # are small, CLS is not affected by your C # code.

1.3.3.2 Garbage collection

The garbage collector is a .NET's memory management method, especially for this issue of how to recycle the memory of the application, the garbage collector provides a good solution. So far, two technologies have been shown on how to release process dynamic applications, two technologies have appeared on the Windows platform.

❑ Let the application code you can manually implement memory release

❑ Let the object to call the number

Let the application code are responsible for releasing memory is low, but high-performance languages, such as C . This method is efficient, and his advantage is that the resources will be released immediately once it is no longer necessary (in general). However, he also brought a lot of shortcomings, thereby triggeting the defects very frequent. The memory of the code application should certainly know the system when they are no longer needed. However, it is very easy to ignore this, never lead to memory leakage.

Although the modern development environment does provide tools that help detect memory leaks, because such issues are only discovered when the Windows refuses to approve the process, it is still difficult to capture. From this point of view, due to the demand for memory, the entire computer will gradually become quite slow.

COM uses the method of maintaining the application count to solve this problem. The method of COM is that each COM component maintains a counter, which saves how many client applications are currently available. When the value of this counter drops to zero, the component will destroy himself and release the relevant memory and resources. But this solution when the problem is still in time to inform the components in a timely manner. They have already used the references. As long as there is a client to destroy yourself in time, the component will stay in memory. However, from some extent, this memory leak may be more serious than C memory leaks, because the COM object will save in its own process and will never be deleted by the system (at least for C memory leakage problem, the system can be All memory resources are retracted while the process is stopped. Now, .NET runtime is changed to rely on the garbage collector to recycle resources. Garbage collector is a program that specializes in organizing memory. The method is to assign all dynamic applications to the pile (this is the same for all languages, but in .NET, CLRs should be used separately to manage themselves for use for .NET applications). The situation is often the case, when .NET detects that the hosted stack assigned to a process has been met, he will call the garbage collector. The garbage collector scans the variables in your code, check the reference to the object on the hosted stack, to determine which one is still in use - that is, which objects have a reference to themselves. Any object that has not been cited without your code will be considered no longer need to be used, so they will be removed. Java also has a similar garbage collector.

The garbage collector in .NET itself is designed to adapt to the work of the process. The principle is: Unless you copy an already existing reference, you can't set a reference for the existing object, and IL is type security. Here is the meaning that if there is a reference to the object, then we have enough information to clearly determine the type of object.

The garbage collection mechanism is not suitable for use in language like non-managed C , for example, C allows the pointer to free conversion in different types.

An important feature of the garbage collection mechanism is that its operating time is uncertain. In other words, you can't guarantee when the garbage collector is called, when the CLR feels that it is necessary to call it, he will be called (unless you know it clearly). Although you can also call the garbage collector directly in your code without considering these processes.

1.3.3.3 Security

Because .NET can provide code-based security mechanisms, .NET is better than Windows in security mechanisms, let alone Windows only provide role-based security.

Role-based security mechanism is based on the account running in the identification process, in other words, who has and run this process? On the other hand, the security of the delivery code is based on how the identification code itself is executed and its own credibility is based on how much credibility is. Fortunately, IL provides a powerful type of security mechanism, and CLR can pre-check the code security before code run to determine the required security license. .NET also provides a mechanism, and the code can pre-indicate what it needs to run.

The importance of code-based security is that she can reduce the risk of codes that are unknown in operation (such as you are in the German code). For example, even the code is running under the administrator account, code-based security can still point out that this code still should not be allowed to perform some administrators should usually be allowed, such as read or write environment variables, read or Write an registry or access .NET reflection characteristics.

Detailed questions in security We will discuss 14 chapters.

1.3.3.4 Application Domain

The application domain is an important reform of .NET. Running applications require both each other and need to communicate with each other, like such a tricky problem, the application domain is design to solve him. The classic case in this regard is a web application server to meet the requests of many of the values. Therefore, the application server may run a lot of different instances in order to give different request services. There are two ways to solve this problem before .NET appears. One is to allow such an instance to share the same process, but this may result in a phenomenon that an instance fail will make the entire web server. Another method is to isolate these instances in different processes, but this also brings a decrease in performance.

So far, we are isolated by running the code in different processes. When you start an application, he is running in a process environment. The Windows system is isolated from each other through the address space. The specific method is that each process has 4GB virtual memory to store his own data and executable code (4GB for 32-bit systems, more than 64-bit systems). Windows indirectly expands the size of the virtual memory by building a virtual memory with the actual physical memory or disk space. Each process has different mappings, and virtual address space block mapping is not overlapping each other (as shown in Figure 1-2).

Figure 1-2

Under normal circumstances, entering the city can only access physical memory through specific addresses in virtual memory, and the process cannot directly access physical memory. This is impossible to have a process that you want to access other processes. This also guarantees that any code with error behavior cannot damage anything other than the address space (note, this security measures are not like on Windows NT / 2000 / XP / 2003 platform. That thoroughly, it is theoretically there is a possibility that the application writes inappropriate memory and causes the possibility of Windows crash).

The purpose of introducing a process is not only an example used to isolate the running code. In the Windows NT / 2000 / XP / 2003 system, the process is still secure permissions and licensed allocation units. Every process has its own security logo, this logo tells Windows what kind of operation is to allow this process to be performed.

Although the process has huge help to security, its maximum disadvantage is to lose performance. Usually, many processes need to work together, so they need to communicate with each other. An obvious example is that there is a process that needs to call a COM component, and this COM component can also be executed, so the COM component needs to run in its own process. There will be similar situations in COM. Since the process does not share memory, a complex job is to copy data between the processes. The result of doing this is to bring huge performance losses. If you want the component to work together, you have to use a DLL-based component technology, so all the code will run in the same address space - of course, the risk is that if One of the components runs wrong, then all programs in this process are turned off.

The purpose of the design application domain is to solve the problem of sharing data between processes without loss of performance. The specific approach is that each process is divided into an application domain. Each application domain corresponds to a hidden separate application, each executed thread runs in a specific application domain (as shown in Figure 1-3).

Figure

1-3

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

New Post(0)