Now, you should know that all the content of CLR is centered on the type, and the type provides the required functions for your application and other types. type also serves as a mechanism for seamless integration of code written in one language with code written in another language. because types are the foundation of CLR, Microsoft has created a formal document description-General Type System (CTS)-to express how types are defined and what their behaviors are.
> Note: In fact, Microsoft has added. other aspects of the Net Framework are submitted to ECMA to form a standard, including the file format, metadata, and IL. Visit the following Platform (P/invoke ), this standard is called the common language infrastructure (CLI ). in addition, Microsoft also submitted some FCL, C # programming languages, and C ++/CLI programming languages. for information on these industrial standards, please go to the ECMA website and refer to www.ecma-international.org/memento/tc39.htm
The CTS specification specifies that a type can contain zero or multiple members:
Fields: a data variable is part of the object state. fields are identified by their names and types.
Method: functions that operate on objects usually change the object state. the method has a name, signature, and modifier. the signature specifies the number and order of parameters, the type of the parameter, and whether the function has a return value. If there is a return value, the type of the function return value is also part of the signature.
Attribute: for callers, this member looks like a field, but for Type implementers, it looks like a method (or two methods ). property allows the real-time user to verify the status of the input parameter and object before accessing the property value and/or calculating the property value. it also allows users of different types to use simple syntax. finally, the attribute allows you to create read-only or write-only fields.
Event: events allow notification between objects. For example, a button provides an event to notify other objects that the button has been clicked.
CTS also specifies rules for Type visibility and access type members. For example, marking a public type will make it visible in any set of programs. on the other hand, marking an assembly type in C # will make it visible in the same assembly. therefore, CTS has rules, and the Assembly forms the border of Type visibility. CLR implements these Visibility rules.
A type is visible to the user and can further limit the ability of the user to access the type members. The following list provides an effective option to control the access members:
PRIVATE: The method can only be called by other functions of this type.
Family: methods are called by derived types. Whether or not they are in a set of programs, note that many languages (such as C ++ and C #) call family as protected.
Family and assembly: The method is called by the derived type, but the derived type definition and the base type are required to be in the same assembly. many languages (such as C # and VB) do not provide this access control. Of course, the Il assembly language has this control.
Assembly: A method is called by any code in the same assembly. Many languages call it internal.
Family or assembly: A method is called by a derived type in any assembly. It can also be called by any type in the same assembly. C # It is called protected internal.
Public: The method can be called by any code in any assembly.
In addition, CTS defines rules such as control type inheritance, virtual functions, and object lifecycle to provide semantic expressions in modern programming languages. in fact, you don't have to learn CTS rules, because the language you choose will give its own programming language syntax and type rules, and you are familiar with these rules, it maps the syntax of a specific language to the language of the IL--CLR when an assembly is generated between compilers.
When I first used CLR, I soon realized that it was best to thinkThe programming language is different from the behavior of your code.. With C ++, you can define your own types and their members. of course, you may have used C # Or VB to define the same type and members. indeed, the syntax for defining types is different, depending on the language you selected, but the type behavior is absolutely the same, because clr cts specifies the type behavior.
To help clarify this idea, let me give you an example. CTS allows a type to be derived from only one base class, therefore, when the C ++ language supports the inheritance of types from multiple base classes, CTS cannot accept such a derived type. to help developers, Microsoft's c ++/CLI compiler reports an error. If it detects your attempt to create hosted code, it contains the types derived from multiple base classes.
This is another rule of CTS. all types must be inherited from a predefined type: system. object. as you can see, the object is the type name in the system namespace, which is the root of all other types, so that each type has at least one group of behaviors. in particular, system. the object type allows you to do the following:
- Compare whether two instances are equal
- Obtains the hash code of an instance.
- Queries the real type of an instance.
- Execute a shortest copy (bitwise copy) for a single strength)
- Obtains the string representation of the current status of an instance object.