GetExecutingAssembly() 和 GetCallingAssembly() 的区别

GetExecutingAssembly() 和 GetCallingAssembly() 的区别

在TCX_1710项目代码的启动项目根目录路径下的Global.asax.cs配置文件中的MVCApplication类中的Application_Start()方法中,配置了项目启动时要加载的项目信息。第一项便是“AppServiceFactory.RegisterServices();”注册所有实现类(该方法为静态方法)。

GetExecutingAssembly() 和 GetCallingAssembly() 的区别

F12进入方法之后为该方法的具体实现,第一步便是Assembly.GetExecutingAssembly(),获取当前方法坐在的程序集;第二步调用别的方法;

那么“Assembly.GetExecutingAssembly()”的值到底是什么呢?打个断点调试一下,结果如下图所示

故,也就是说,“Assembly.GetExecutingAssembly()”这句代码表示,当前方法(RegisterServices()方法)所在的程序集为“Ace”程序集。

GetExecutingAssembly() 和 GetCallingAssembly() 的区别

到底是不是"Ace"程序集呢?会过头看看就知道了,如下图所示。

命名空间为“Ace.Application”,注意"Application"只是"Ace"程序集下的一个文件夹而已,真正的程序集就是“Ace”!!!

GetExecutingAssembly() 和 GetCallingAssembly() 的区别

 那么“GetCallingAssembly()”又是什么呢?试一试就知道了

将“Assembly assembly = Assembly.GetExecutingAssembly();”修改为“Assembly assembly = Assembly.GetCallingAssembly();”打个断点,结果如下图所示。

也就是说,“Assembly.GetCallingAssembly();”这句代码表示,【调用当前方法(当前方法为RegisterServices()方法)的方法】(调用当前方法的方法为Application_Start()方法)所在的程序集为“DoMes.Web”

GetExecutingAssembly() 和 GetCallingAssembly() 的区别

总结 :

Assembly.GetExecutingAssembly():当前方法所在程序集

Assembly.GetCallingAssembly():调用当前方法的方法 所在的程序集