加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – GetCallingAssembly()和GetExecutingAssembly()是否同样倾

发布时间:2020-12-15 04:29:42 所属栏目:百科 来源:网络整理
导读:有 Assembly.GetExecutingAssembly()和 Assembly.GetCallingAssembly().请注意,GetCallingAssembly()有一个备注,提到根据JIT内联的行为方式,一个方法可能(或没有)内联到另一个方法中,因此GetCallingAssembly()返回不同的结果. 现在GetExecutingAssembly()有
有 Assembly.GetExecutingAssembly()和 Assembly.GetCallingAssembly().请注意,GetCallingAssembly()有一个备注,提到根据JIT内联的行为方式,一个方法可能(或没有)内联到另一个方法中,因此GetCallingAssembly()返回不同的结果.

现在GetExecutingAssembly()有何不同? JIT内联在技术上可以内联调用GetExecutingAssembly()的代码,因此代码现在属于不同的程序集,并且取决于是否发生GetExecutingAssembly()也可以产生不同的结果.

为什么GetExecutingAssembly()描述没有提到类似于GetCallingAssembly()描述的JIT inining?

解决方法

GetExecutingAssembly方法不容易受到JIT内联的影响,因为同样的原因,MethodBase.GetCurrentMethod也不会受到影响,因为它们是以类似的方式实现的.

这两个方法都声明了一个特殊枚举StackCrawlMark的局部变量,并将其初始化为StackCrawlMark.LookForMyCaller.此局部变量具有防止调用GetExecutingAssembly或GetCurrentMethod的方法被内联的副作用,这将保证正确的结果.

这可以通过实验以及SSCLI20中与此枚举相关的注释来支持:

// declaring a local var of this enum type and passing it by ref 
// into a function that needs to do a stack crawl will both prevent inlining of 
// the calle and pass an ESP point to stack crawl to
//
// Declaring these in EH clauses is illegal; 
// they must declared in the main method body

GetCallingAssembly易受影响的原因是因为您正在寻找调用者的调用者,并且局部变量仅保证调用者没有内联,这意味着可以内联祖父素方法导致意外结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读