MSIL 方法中 hidebysig 的用途是什么?
使用 ildasm 和 C# 程序,例如
static void Main(string[] args)
{
}
给出:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Program::Main
hidebysig 构造有什么作用?
Using ildasm and a C# program e.g.
static void Main(string[] args)
{
}
gives:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Program::Main
What does the hidebysig construct do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 ECMA 335,分区 1 的第 8.10.4 节:
(目前尚不清楚,但
hidebysig
的意思是“通过名称和签名隐藏”。)同样在分区 2 的第 15.4.2.2 节中:
举个例子,假设您有:
这是有效的,因为
Bar(string)
不会 隐藏Bar()
,因为 C# 编译器使用hidebysig
。 如果它使用“按名称隐藏”语义,则您根本无法对Derived
类型的引用调用Bar()
,尽管您仍然可以对其进行强制转换到基地并这样称呼它。编辑:我刚刚尝试过将上述代码编译为 DLL,对其进行 ildasming,删除
Bar()
和Bar(string)
hidebysig code>,再次对其进行 ilasming,然后尝试从其他代码调用Bar()
:但是:(
没有编译问题。)
From ECMA 335, section 8.10.4 of partition 1:
(It's not immediately clear from that, but
hidebysig
means "hide by name-and-signature".)Also in section 15.4.2.2 of partition 2:
As an example, suppose you have:
That's valid, because
Bar(string)
doesn't hideBar()
, because the C# compiler useshidebysig
. If it used "hide by name" semantics, you wouldn't be able to callBar()
at all on a reference of typeDerived
, although you could still cast it to Base and call it that way.EDIT: I've just tried this by compiling the above code to a DLL, ildasming it, removing
hidebysig
forBar()
andBar(string)
, ilasming it again, then trying to callBar()
from other code:However:
(No compilation problems.)
根据 THE SKEET 的回答,另外原因是 Java 和 C# 允许类的客户端调用任何具有相同名称的方法,包括来自基类的方法。 而 C++ 则不然:如果派生类定义了一个与基类中的方法同名的方法,那么客户端就不能直接调用基类方法,即使它不采用相同的参数。 因此,CIL 中包含了该功能以支持这两种重载方法。
在 C++ 中,您可以使用
using
指令从基类有效导入一组命名的重载,以便它们成为该方法名称的“重载集”的一部分。As per THE SKEET's answer, in addition the reason for this is that Java and C# allow the client of a class to call any methods with the same name, including those from base classes. Whereas C++ does not: if the derived class defines even a single method with the same name as a method in the base class, then the client cannot directly call the base class method, even if it doesn't take the same arguments. So the feature was included in CIL to support both approaches to overloading.
In C++ you can effectively import one named set of overloads from the base class with a
using
directive, so that they become part of the "overload set" for that method name.根据微软文档
According to Microsoft Docs