IL知道什么是接口吗

本文关键字:接口 什么 IL | 更新日期: 2023-09-27 18:27:14

例如,当你在源代码中引入一个静态类时,csc编译器会把它变成一个密封的抽象类(如果我错了,请纠正我)。

但是界面怎么样

IL知道什么是接口吗

例如,当您在源代码中引入抽象类时,csc编译器将它变成一个密封的静态类(如果我是错了)。

我在纠正你。

抽象类,例如:

public abstract class Foo
{
}

在IL中看起来是这样的:

.class public abstract auto ansi beforefieldinit Foo
    extends [mscorlib]System.Object
{
    .method family hidebysig specialname rtspecialname instance void .ctor() cil managed
    {
    }
}

但是界面怎么样;CLR知道什么是接口吗?

是的,它知道。例如:

public interface IFoo
{
}

翻译为:

.class public interface abstract auto ansi IFoo
{
}

您错了,是static class转换为等效的sealed abstract class

接口是.NET的完全成员,具有不同于任何其他类型的不同元数据和行为(例如多重继承)。

以下是ILSpy为以下代码显示的内容。

C#:

interface A
{
    void M();
}

IL:

.class interface nested private auto ansi abstract A
{
    // Methods
    .method public hidebysig newslot abstract virtual 
        instance void M () cil managed 
    {
    } // end of method A::M
} // end of class A

所以,是的,它确实知道。

这就是CLR的接口:

.class public interface abstract auto ansi IDisposable
{
    .custom instance void System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = { bool(true) }
    .method public hidebysig newslot abstract virtual instance void Dispose() cil managed
    {
    }
}

以IDisposable为例。