我好奇的是,其中 Type.GetType()
实现,所以我把偷看的组装,并注意到 Type.GetType( )
电话 base.GetType()
键,因为键入
从继承的MemberInfo
我接过来一看,它被定义为 _MemberInfo.GetType()
返回 this.GetType()
。因为我无法找到实际的code,显示了C#可以获得类型的信息,我想知道:
I got curious as to where Type.GetType()
is implemented, so I took a peek at the assembly and noticed Type.GetType()
calls base.GetType()
and since Type
inherits from MemberInfo
I took a look and it is defined as _MemberInfo.GetType()
which returns this.GetType()
. Since I cannot find the actual code that shows how C# can get type information I would like to know:
如何从对象的CLR GET类型和的MemberInfo在运行?
How does the CLR get Type and MemberInfo from objects at Runtime?
下面的实际源用于.NET Framework 2.0,可在Internet(用于教育目的)上:的 http://www.microsoft.com/en-us/download/details.aspx?id=4917
The ACTUAL source for .NET Framework 2.0 is available on the internet (for educational purposes) here: http://www.microsoft.com/en-us/download/details.aspx?id=4917
这是C#语言实现。你可以使用7zip的来解压。您将在这里找到的反射命名空间(相对):
This is the C# Language implementation. You can use 7zip to unpack it. You will find the reflection namespace here (relatively):
sscli20 CLR的 src BCL SYSTEM 反射
.sscli20clrsrcbclsystemreflection
我挖你问一下具体的实现,但这是一个好的开始。
I am digging for the specific implementation you are asking about, but this is a good start.
更新:很抱歉,但我认为它是一个死胡同。 Type.GetType()
调用基实现它来源于System.Object的。如果你检查了$ C $的CFile(。 sscli20 CLR的 src BCL SYSTEM object.cs
),你会发现该方法为extern
(见下面code)。进一步检查可能发现的实施,但它不是在BCL。我怀疑它会在C ++ code的地方。
UPDATE: Sorry, but I think its a dead end. Type.GetType()
calls to the base implementation which comes from System.Object. If you inspect that codefile (.sscli20clrsrcbclsystemobject.cs
) you will find the method is extern
(see code below). Further inspect could uncover the implementation, but its not in the BCL. I suspect it will be in C++ code somewhere.
// Returns a Type object which represent this object instance.
//
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern Type GetType();
更新(再次):我进一步挖掘,最终发现在CLR虚拟机本身的实现答案。 (它在C ++中)。
UPDATE (AGAIN): I dug deeper and found the answer in the implementation of the CLR virtual machine itself. (Its in C++).
第一块拼图是在这里:
sscli20 CLR的 src 虚拟机 ecall.cpp
sscli20clrsrcvmecall.cpp
在这里,我们看到了code映射到一个C ++函数的外部调用。
Here we see the code that maps the external call to an C++ function.
FCFuncStart(gObjectFuncs)
FCIntrinsic("GetType", ObjectNative::GetClass, CORINFO_INTRINSIC_Object_GetType)
FCFuncElement("InternalGetHashCode", ObjectNative::GetHashCode)
FCFuncElement("InternalEquals", ObjectNative::Equals)
FCFuncElement("MemberwiseClone", ObjectNative::Clone)
FCFuncEnd()
现在,我们需要去寻找 ObjectNative ::的getclass
...这是在这里:
Now, we need to go find ObjectNative::GetClass
... which is here:
sscli20 CLR的 src 虚拟机 comobject.cpp
sscli20clrsrcvmcomobject.cpp
和这里是实施的GetType
:
FCIMPL1(Object*, ObjectNative::GetClass, Object* pThis)
{
CONTRACTL
{
THROWS;
SO_TOLERANT;
DISABLED(GC_TRIGGERS); // FCallCheck calls ForbidenGC now
INJECT_FAULT(FCThrow(kOutOfMemoryException););
SO_TOLERANT;
MODE_COOPERATIVE;
}
CONTRACTL_END;
OBJECTREF objRef = ObjectToOBJECTREF(pThis);
OBJECTREF refType = NULL;
TypeHandle typeHandle = TypeHandle();
if (objRef == NULL)
FCThrow(kNullReferenceException);
typeHandle = objRef->GetTypeHandle();
if (typeHandle.IsUnsharedMT())
refType = typeHandle.AsMethodTable()->GetManagedClassObjectIfExists();
else
refType = typeHandle.GetManagedClassObjectIfExists();
if (refType != NULL)
return OBJECTREFToObject(refType);
HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_RETURNOBJ, objRef, refType);
if (!objRef->IsThunking())
refType = typeHandle.GetManagedClassObject();
else
refType = CRemotingServices::GetClass(objRef);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(refType);
}
FCIMPLEND
最后一件事,对 GetTypeHandle
以及其他一些辅助功能的实现可以在这里找到:
One last thing, the implementation of GetTypeHandle
along with some other supporting functions can be found in here:
sscli20 CLR的 src 虚拟机 object.cpp
sscli20clrsrcvmobject.cpp