C # knowledge point

Source: Internet
Author: User

The program code is compiled to generate an executable application. First, we need to understand the structure of this executable application.

The application structure can be divided into application domain-assembly-module-type-member layers, and the common language runtime loader manages the application domain, such management includes loading each assembly to the corresponding application domain and controlling the memory layout of the type hierarchy of each set.

C # application domain:

In traditional development, we all know that an application corresponds to a process and specifies virtual memory for the process. The operating system maps the actual physical memory, it effectively maintains the security between processes. On the other hand, every process consumes a certain amount of system resources, reduces performance, and makes communication between processes troublesome.

A new concept is introduced in. Net: C # application domain (appdomain ). It can be understood that many application domains can run in the same. Net process, which can reduce system consumption and isolate different domains to ensure security. In addition, communication between different domains in the same process is relatively simple.

C # reflection mechanism:

Reflection definition: the ability to review metadata and collect information about its type. Metadata (the basic data unit after editing) is a large number of tables, and the compiler creates a class definition table, A field definition table, a method definition table, and so on. the reflection namespace contains several classes that allow you to reflect (PARSE) the Code of these metadata

 

I. Functions of reflection:

Dynamically create an instance of the type, specify the type to the existing object, or obtain the type from the existing object.

The application needs to load a specific type from a specific set of programs at runtime, so that reflection can be used for a task.

Reflection is mainly used in class libraries. These class libraries need to know a type definition to provide more functions.

 

II. Key points of application:

Reflection is rarely used in real applications.

Dynamic reflection binding sacrifices performance.

Some metadata information cannot be obtained through reflection.

Some reflection types are specially developed and used for those CLR development editors, so you must realize that not all reflection types are usable.

 

3. Methods for obtaining assembly:

Assembly. Load

Assembly. LoadFile

Assembly. loadfrom

Assembly Method of type object

 

Iv. Reflection members:

Memberinfo-member

Constructorinfo-structure

Fieldinfo-field

Methodinfo-Method

Propertyinfo-Properties

Eventinfo-Event

 

Reflection performance:

When Using Reflection to call a type or trigger method, or when accessing a field or attribute, CLR needs to do more work: Check parameters, check permissions, and so on, so the speed is very slow. Therefore, do not use reflection for programming. You can use the following methods to write a dynamic Constructor (late binding:

1. inherit from the class. Let this type be derived from a known basic type during compilation, generate an instance of this type at runtime, and place its reference to a variable of its basic type, then, call the virtual method of the basic type.

2. Implemented through interfaces. At runtime, construct an instance of this type, place its reference to a variable of its interface type, and then call the virtual method defined by this interface.

3. Implemented through delegation. Let this type implement a method. Its name and prototype are consistent with a delegate that is known during compilation. Construct an instance of this type at runtime, then construct the instance of the delegate using the object and name of the method, and then call the method you want through the delegate. This method is more efficient than the previous two methods.

Relationships between namespaces and accessories:

Many people may not be clear about this concept. It is necessary to clarify this concept for qualified. Net programmers.

The namespace is similar to a Java package, but it is not exactly the same, because the Java package must be placed according to the directory structure, and the namespace is not required.

The assemblies are the units where the .netapp executes. All the compiled .dllw..exe files are accessories.

The relationship between accessories and namespaces is not one-to-one, and they are not mutually exclusive. One Accessory can have multiple namespaces, and one namespace can also exist in multiple accessories.

 

C # Packing and unboxing:

 

Converting a value-type instance to an instance of the reference class is called packing. The inverse process is called unboxing.

(Binning converts the value type to the reference type, and unboxing converts the reference type to the value type)

. In. net, the data type is divided into value type and reference (not the same as the pointer of C ++) type. Correspondingly, the memory allocation is divided into two methods: Stack, second, heap. Note: it is a managed heap.

The value type is only allocated in the stack.

Allocate memory and managed heap for reference types.

The managed heap corresponds to garbage collection.

Packing: used to store value types in the garbage collection heap. Packing is an implicit conversion between the value type and the object type or any interface type implemented by this value type.

Unpacking: the explicit conversion from the object type to the value type or from the interface type to the value type of the interface.




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.