After going through the first three articles. net Framework source code Research Series, I believe everyone found in fact. the implementation of the Net Framework is not complex. It may be similar to the project development we have made. this is my opinion. however, after careful and in-depth research, we will still find things that are hard to notice at ordinary times, and these things will improve our thinking and broaden our horizons, it is of great significance to exercise a good coding quality.
We know. all types (including reference type and value type) in. NET are derived from the object class. Therefore, object is the foundation of all types. so today we will study. the root of all elements in net (C #) --- system. object.
The implementation of object classes in. net source code is very simple. There are only about 100 lines in total.CodeThis is also the focus for us to carefully study every line of code. Let's take a look at its definition first.
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 [serializable ()]
2 [classinterface (classinterfacetype. autodual)]
3 [system. runtime. interopservices. comvisible ( true )]
4 Public class Object
By definition, an object seems to be a common class (does it mean that an object is also derived from an object ?! ^_^ ). The serializable label indicates that the object can be serialized and deserialized. classinterface (classinterfacetype. autodual), system. runtime. interopservices. comvisible (true) is. net to support the special design of COM. unless you want to manually wrap COM, it makes no sense for people who mostly use hosted code.
Next, let's look at its constructor:
1 [Reliabilitycontract (consistency. willnotcorruptstate, Cer. mayfail)]
2 PublicObject (){ }
3[Reliabilitycontract (consistency. willnotcorruptstate, Cer. Success)]
4~ Object (){}
From the code above, we can see that the object only has a constructor with an empty method body. This is the origin of the universe, and there is nothing at the beginning. here we need to note the reliabilitycontract label.Reliabilitycontract defines the availability between the author of some code and the developer dependent on the code.This is an official definition. It sounds a bit mysterious.Reliabilitycontract has two attributes: CER and consistencyguarantee.. Cer refers
Specifies the behavior of a method when calling within the restricted execution area; consistencyguarantee indicates the reliability agreement. After reading the description of the two enumeration types in msdn, we found that[Reliabilitycontract (consistency. willnotcorruptstate, Cer. mayfail)] provides security for the object. This means that the execution result is always known no matter whether an exception occurs when calling the object constructor.
In addition, the object explicitly defines the Destructor (it is also not recommended. Microsoft seems to like it very much and does not recommend what we do @ _ @ | ), this allows us to manually release the resources occupied by objects without waiting for the GC to be automatically released.
It is worth noting that there is a concept we rarely encounter:CER, the restricted execution area, is part of the mechanism for creating reliable hosted code. The CER defines a region. In this region, the Common Language Runtime Library (CLR) is constrained and cannot cause an out-of-band exception that prevents code in the region from being fully executed. In this area, user code is restricted and cannot be executed, leading to out-of-band exceptions.
After reading the constructor, we can see the implementation of the three most commonly used methods in the object. The Code is as follows:
Code
Public Virtual String tostring (){
Return GetType (). tostring ();
}
[Methodimplattritions (methodimploptions. internalcall)]
Public Extern Type GetType ();
Public Virtual Bool Equals (Object OBJ ){
Return Internalequals ( This , OBJ );
}
[Methodimplattritions (methodimploptions. internalcall)]
Internal Static Extern Bool Internalequals (Object obja, object objb );
Public Virtual Int Gethashcode (){
Return Internalgethashcode ( This );
}
[Methodimplattritions (methodimploptions. internalcall)]
Internal Static Extern Int Internalgethashcode (Object OBJ );
I feel a tragedy, because these three methods have nothing to do and directly call the internal CLR method. It seems that the object is also a vest :(. microsoft officially claims that the object class is the base class of all classes. I am afraid I will ask a question mark here. I guess this object is not another object. maybe the CLR also has an object.
If the above three methods are a bit speechless, the following code is even more confusing.
Code
Private Void Fieldsetter (string typename, string fieldname, object Val ){
Fieldinfo fldinfo = Getfieldinfo (typename, fieldname );
If (Fldinfo. isinitonly)
Throw New Fieldaccessexception (environment. getresourcestring ( " Fieldaccess_initonly " ));
System. runtime. remoting. messaging. Message. coercearg (Val, fldinfo. fieldtype );
Fldinfo. setvalue ( This , Val );
}
Private Void Fieldgetter (string typename, string fieldname, Ref Object Val ){
Fieldinfo fldinfo = Getfieldinfo (typename, fieldname );
Val = Fldinfo. getvalue ( This );
}
Private Fieldinfo getfieldinfo (string typename, string fieldname ){
Type T = GetType ();
While ( Null ! = T ){
If (T. fullname. Equals (typename )){
Break ;
}
T = T. basetype;
}
If ( Null = T ){
Throw New Remotingexception (string. Format (cultureinfo. currentculture, environment. getresourcestring ( " Remoting_badtype " ), Typename ));
}
Fieldinfo fldinfo = T. getfield (fieldname, bindingflags. Public | Bindingflags. Instance | Bindingflags. ignorecase );
If ( Null = Fldinfo ){
Throw New Remotingexception (string. Format (cultureinfo. currentculture, environment. getresourcestring ( " Remoting_badfield " ), Fieldname, typename ));
}
Return Fldinfo;
}
The above Code seems to read or set the value of a field (we also do this ). when the problem occurs, I did not find the places where these methods are used in the object, and private decides that the extension class cannot be accessed. so what are the purposes of these three methods? I'm afraid only Microsoft knows.
Summary
After the above analysis, we found that the object implementation is actually very simple and there is almost no content. However, the CER in the object brings us a bit of thinking about writing high-security code.