Net Assembly.GetExecutingAssembly() 和 Assembly.GetCallingAssembly()的区别

Net Assembly.GetExecutingAssembly() 和 Assembly.GetCallingAssembly()的区别

2011-06-18 19:43

GetExecutingAssembly():是指获取调用此方法(GetExecutingAssembly)的方法所在的程序集。

GetCallingAssembly():是指获取调用此方法(GetCallingAssembly)所在方法的方法的程序集。

举例来说:

程序集:Test.dll 有如下类:

namespace Test
{
    public class TestClass
    {

        public static void GetAssemblyName()
        {
            Console.WriteLine(Assembly.GetExecutingAssembly().FullName);
            Console.WriteLine();
            Console.WriteLine(Assembly.GetCallingAssembly().FullName);
        }
    }
}
在控制台应用程序TestAssembly中引用Test.dll程序集,主程序如下:

namespace AssemblyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Test.TestClass.GetAssemblyName();     
        }

    }
}

输出结果:

Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

AssemblyTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

根据结果在好好回味前两句话,就很清楚它们之间的区别了。

原文地址:https://www.cnblogs.com/cpetcoandy/p/2172273.html