MSIL方法中hidebysig的目的是什么? [英] What is the purpose of hidebysig in a MSIL method?

查看:91
本文介绍了MSIL方法中hidebysig的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ildasm和C#程序,例如

Using ildasm and a C# program e.g.

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构造做什么?

What does the hidebysig construct do?

推荐答案

来自 ECMA 335 ,分区1的8.10.4节:

From ECMA 335, section 8.10.4 of partition 1:


CTS对从基本类型(隐藏)和
可见的
名称提供独立控制
共享派生的
类中的版位槽(覆盖)。隐藏是通过将
派生类中的成员标记为按名称隐藏
或按名称和签名隐藏来控制
。隐藏
总是根据成员的类型
执行,也就是说,派生字段
的名称可以隐藏基本字段名称,但是
不能隐藏方法名称,属性名称或
事件名称。如果派生成员是按名称隐藏的
,则在
派生类中不可见基类中同类型
成员且同名
的成员;如果成员通过名称和签名被标记为
隐藏,则只有相同类型的
成员具有与名称和类型完全相同的
(对于字段)或
方法签名(对于方法)是
对派生类隐藏的。
这两种隐藏形式之间的区别
的实现完全是由源语言
编译器和反射库提供的

对VES
本身没有直接影响。

The CTS provides independent control over both the names that are visible from a base type (hiding) and the sharing of layout slots in the derived class (overriding). Hiding is controlled by marking a member in the derived class as either hide by name or hide by name-and-signature. Hiding is always performed based on the kind of member, that is, derived field names can hide base field names, but not method names, property names, or event names. If a derived member is marked hide by name, then members of the same kind in the base class with the same name are not visible in the derived class; if the member is marked hide by name-and-signature then only a member of the same kind with exactly the same name and type (for fields) or method signature (for methods) is hidden from the derived class. Implementation of the distinction between these two forms of hiding is provided entirely by source language compilers and the reflection library; it has no direct impact on the VES itself.

(目前尚不清楚,但是 hidebysig 表示按名称和签名隐藏。)

(It's not immediately clear from that, but hidebysig means "hide by name-and-signature".)

也在分区2的15.4.2.2节中:

Also in section 15.4.2.2 of partition 2:


hidebysig是为使用
工具提供的,被VES忽略。
指定已声明的方法
隐藏具有匹配方法
签名的基类
类型的所有方法;如果忽略,则方法
应该隐藏具有相同
名称的所有方法,无论其签名如何。

hidebysig is supplied for the use of tools and is ignored by the VES. It specifies that the declared method hides all methods of the base class types that have a matching method signature; when omitted, the method should hide all methods of the same name, regardless of the signature.

例如,假设您有:

public class Base
{
    public void Bar()
    {
    }
}

public class Derived : Base
{
    public void Bar(string x)
    {
    }
}

...

Derived d = new Derived();
d.Bar();

这是有效的,因为 Bar(string) 不会隐藏 Bar(),因为C#编译器使用 hidebysig 。如果它使用按名称隐藏语义,则将完全无法在 Derived类型的引用上调用 Bar()。 code>,尽管您仍然可以将其转换为Base并以这种方式调用。

That's valid, because Bar(string) doesn't hide Bar(), because the C# compiler uses hidebysig. If it used "hide by name" semantics, you wouldn't be able to call Bar() at all on a reference of type Derived, although you could still cast it to Base and call it that way.

编辑:我只是通过将以上代码编译为DLL,对其进行ildasming,删除 Bar() Bar(string) hidebysig code>,再次释放它,然后尝试从其他代码中调用 Bar()

I've just tried this by compiling the above code to a DLL, ildasming it, removing hidebysig for Bar() and Bar(string), ilasming it again, then trying to call Bar() from other code:

Derived d = new Derived();
d.Bar();

Test.cs(6,9): error CS1501: No overload for method 'Bar' takes '0' arguments

但是:

Base d = new Derived();
d.Bar();

(没有编译问题。)

这篇关于MSIL方法中hidebysig的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆