怎么看$ C $,这标志着作为MethodImplOptions.InternalCall方法C吗? [英] How to see code of method which marked as MethodImplOptions.InternalCall?

查看:325
本文介绍了怎么看$ C $,这标志着作为MethodImplOptions.InternalCall方法C吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ILSpy检查System.String的code,我发现有标记为MethodImplOptions.InternalCall一些方法,如:

When use ILSpy to check the code of System.String, I found there are some methods marked as MethodImplOptions.InternalCall such as:

[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int nativeCompareOrdinalEx(string strA, int indexA, string strB, int indexB, int count);

我知道MethodImplOptions.InternalCall意味着这个方法是由公共语言运行时优化code本地实现来提高性能。

I know MethodImplOptions.InternalCall means this method is implemented natively by the common language runtime to optimized code to improve performance.

我的问题是:是不是无论如何可以使我们看到了code标记为MethodImplOptions.InternalCall

推荐答案

您需要的源$ C ​​$ C为CLR看到这些方法的实现。这是一个有点来之不易,微软并没有公布它,它是不包括在参考源。

You'll need the source code for the CLR to see the implementation of these methods. That's a bit hard to come by, Microsoft doesn't publish it and it is not covered by the Reference Source.

只要方法是老,可因为.NET 2.0,那么你将有一个镜头在它的的SSCLI20源$ C ​​$ C 。对于非零风险,你将看到的是一个过时的版本,当然在code。但足以让一个想法是什么样子,往往还是准确的。

As long as the method is "old", available since .NET 2.0, then you'll have a shot at it from the SSCLI20 source code. With a non-zero risk that you will be looking at an outdated version of the code of course. But good enough to get an idea what it looks like and often still accurate.

的起点,开始搜索code是CLR / src目录/ VM / ecall.cpp源$ C ​​$ C文件。它包含的抖动搜索内部方法的表。本节与自己相关的nativeCompareOrdinalEx()看起来是这样的:

The starting point to start searching for the code is the clr/src/vm/ecall.cpp source code file. It contains the tables that the jitter searches for internal methods. The section that's relevant for nativeCompareOrdinalEx() looks like this:

FCFuncStart(gStringFuncs)
    FCDynamic("FastAllocateString", CORINFO_INTRINSIC_Illegal, ECall::FastAllocateString)
    FCDynamicSig(COR_CTOR_METHOD_NAME, &gsig_IM_ArrChar_RetVoid, CORINFO_INTRINSIC_Illegal, ECall::CtorCharArrayManaged)
    FCDynamicSig(COR_CTOR_METHOD_NAME, &gsig_IM_ArrChar_Int_Int_RetVoid, CORINFO_INTRINSIC_Illegal, ECall::CtorCharArrayStartLengthManaged)
    FCDynamicSig(COR_CTOR_METHOD_NAME, &gsig_IM_PtrChar_RetVoid, CORINFO_INTRINSIC_Illegal, ECall::CtorCharPtrManaged)
    FCDynamicSig(COR_CTOR_METHOD_NAME, &gsig_IM_PtrChar_Int_Int_RetVoid, CORINFO_INTRINSIC_Illegal, ECall::CtorCharPtrStartLengthManaged)
    FCDynamicSig(COR_CTOR_METHOD_NAME, &gsig_IM_Char_Int_RetVoid, CORINFO_INTRINSIC_Illegal, ECall::CtorCharCountManaged)

    FCFuncElement("nativeCompareOrdinal", COMString::FCCompareOrdinal)     // <=== Here
    FCFuncElement("nativeCompareOrdinalWC", COMString::FCCompareOrdinalWC)
    FCIntrinsic("get_Length", COMString::Length, CORINFO_INTRINSIC_StringLength)
    // etc..
}

请注意该FCFuncElement有着怎样的方法名称作为一个字符串和一个函数指针,以实现内部调用C ++的方法。 Grepping源$ C ​​$ C树,然后把你带到CLR / src目录/ VM / comstring.cpp。我就不再一一每个人都用C ++ code,只要看看自己。

Note how the FCFuncElement has the method name as a string and a function pointer to the C++ method that implements the internal call. Grepping the source code tree then takes you to clr/src/vm/comstring.cpp. I won't bore everybody with the C++ code, just have a look for yourself.

/*================================CompareOrdinal===============================*/
FCIMPL3(INT32, COMString::FCCompareOrdinal, StringObject* strA, StringObject* strB, CLR_BOOL bIgnoreCase) {
    // Yadayada
    //...
}

在搜索CaseInsensitiveCompHelper()和FastCompareStringHelperAligned(),把你带到的分别,不区分大小写和区分大小写的比较函数的实际实现在同一个源$ C ​​$ C文件。

Searching for CaseInsensitiveCompHelper() and FastCompareStringHelperAligned() takes you to the actual implementations of the, respectively, case-insensitive and case-sensitive comparison functions in the same source code file.

唯一的其他东西值得注意的关于这是CLR版本4做了一些改变这一机制。添加很多新的内部方法,并通过一个名为QCall假的DLL中的[的DllImport]属性支持一个完全不同的另外的互操作机制。有看到源代码,这些附加据我所知,没有什么好办法。

The only other thing notable about this is that CLR version 4 made some changes to this mechanism. Adding lots of new internal methods and supporting an entirely different additional interop mechanism through a [DllImport] attribute for a fake DLL named "QCall". There's no good way to see source for these additions that I know of.

这篇关于怎么看$ C $,这标志着作为MethodImplOptions.InternalCall方法C吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆