0

JITインライン化が発生する「HelloWorld」サイズのC#コードスニペットを作成しようとしています。これまでのところ私はこれを持っています:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine( GetAssembly().FullName );
        Console.ReadLine();
    }

    static Assembly GetAssembly()
    {
        return System.Reflection.Assembly.GetCallingAssembly();
    }
}

これは、VisualStudioから「リリース」-「任意のCPU」および「デバッグなしで実行」としてコンパイルします。サンプルプログラムアセンブリの名前が表示されるので、明確にGetAssembly()インライン化されていません。Main()そうでない場合は、mscorlibアセンブリ名が表示されます。

JITインライン化が発生するC#コードスニペットを作成するにはどうすればよいですか?

4

2 に答える 2

6

確かに、ここに例があります:

using System;

class Test
{
    static void Main()
    {
        CallThrow();
    }

    static void CallThrow()
    {
        Throw();
    }

    static void Throw()
    {
        // Add a condition to try to disuade the JIT
        // compiler from inlining *this* method. Could
        // do this with attributes...
        if (DateTime.Today.Year > 1000)
        {
            throw new Exception();
        }
    }
}

リリースのようなモードでコンパイルします。

csc /o+ /debug- Test.cs

走る:

c:\Users\Jon\Test>test

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
   at Test.Throw()
   at Test.Main()

スタックトレースに注意してください。のコードがインライン化されているため、Throwによって直接呼び出されたように見えます。MainCallThrow

于 2012-10-02T08:36:33.373 に答える
1

インライン化についてのあなたの理解は間違っているようです: インライン化GetAssemblyされていても、プログラムの名前が表示されます。

インライン化とは、「関数呼び出しの場所で関数の本体を使用する」ことを意味します。インラインGetAssembly化すると、次のようなコードになります。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(System.Reflection.Assembly.GetCallingAssembly()
                                                    .FullName);
        Console.ReadLine();
    }
}
于 2012-10-02T08:31:04.433 に答える