如果Reflector

时间:2017-05-13 07:27:22

标签: c# .net decompiling reflector .net-reflector

每隔一段时间我就会写一个程序来提醒自己一些东西,所以这就是我昨天写的一个奇怪的事情。

原始资料

using System;
using System.Threading;

namespace TestThreadFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "Main thread";

            var t = JoiningAnAlreadyCompletedThread();

            JoiningIsIdempotent(t);

            Console.ReadKey();
        }

        static Thread JoiningAnAlreadyCompletedThread()
        {
            Thread t = new Thread(() =>
            {
                Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I'm done now.\n");
            })
            { Name = "Worker 1" };

            t.Start();

            // Give a long time-out so we know that the 
            // current thread did not wait the entire time-out
            // or that it didn't wait at all because the thread
            // we are joining on has already completed.
            t.Join(10000);

            Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n");

            return t;
        }

        static void JoiningIsIdempotent(Thread threadToJoinOn)
        {
            Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n");

            // Joining is idempotent
            threadToJoinOn.Join();

            Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n");
        }
    }
}

当我在Reflector中查找程序时,它在if方法的开头有这个奇怪的JoiningAnAlreadyCompletedThread条件,如下所示:

private static Thread JoiningAnAlreadyCompletedThread()
{
    if (<>c.<>9__1_0 == null)
    {
        ThreadStart start1 = <>c.<>9__1_0;
    }

    ...
}

我读过几次只是为了确认我没有遗漏某些东西。这很奇怪,什么也没做。

仅仅为了完整起见,这里是Reflector完全反编译的程序。

反射器反编译源

internal class Program
{
    // Methods
    private static Thread JoiningAnAlreadyCompletedThread()
    {
        if (<>c.<>9__1_0 == null)
        {
            ThreadStart start1 = <>c.<>9__1_0;
        }
        Thread thread1 = new Thread(<>c.<>9__1_0 = new ThreadStart(<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0)) {
            Name = "Worker 1"
        };
        Thread t = thread1;
        t.Start();
        t.Join(0x2710);
        Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
        return t;
    }

    private static void JoiningIsIdempotent(Thread threadToJoinOn)
    {
        Console.WriteLine(string.Format("{0} ({1}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
        threadToJoinOn.Join();
        Console.WriteLine(string.Format("{0} ({1}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
    }

    private static void Main(string[] args)
    {
        Thread.CurrentThread.Name = "Main thread";
        JoiningIsIdempotent(JoiningAnAlreadyCompletedThread());
        Console.ReadKey();
    }

    // Nested Types
    [Serializable, CompilerGenerated]
    private sealed class <>c
    {
        // Fields
        public static readonly Program.<>c <>9 = new Program.<>c();
        public static ThreadStart <>9__1_0;

        // Methods
        internal void <JoiningAnAlreadyCompletedThread>b__1_0()
        {
            Console.WriteLine(string.Format("{0} ({1}): I'm done now.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
        }
    }
}

我在IL Spy中查找了相同的来源,并没有做到这一点奇怪的事情。没有这种奇怪的if条件没有做任何事情。

IL间谍反编译

// TestThreadFunctions.Program
private static Thread JoiningAnAlreadyCompletedThread()
{
    ThreadStart arg_20_0;
    if ((arg_20_0 = Program.<>c.<>9__1_0) == null)
    {
        arg_20_0 = (Program.<>c.<>9__1_0 = new ThreadStart(Program.<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0));
    }
    Thread thread = new Thread(arg_20_0)
    {
        Name = "Worker 1"
    };
    thread.Start();
    thread.Join(10000);
    Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
    return thread;
}

所以,我想我不知道这里的问题是什么。可能是“为什么Reflector会这样做?”

然后可能答案是,“嗯,反编译很难”,我理解。

所以,我真的不知道问题是什么,但我想我只是想分享我的困惑。

如果你真的在这里看到一个有合理解释的问题,我很想知道为什么Reflector会这样做。

否则,我知道这个问题会被关闭并关闭。

0 个答案:

没有答案