簡體   English   中英

IL知道INTERFACE是什么嗎?

[英]Does the IL knows what an INTERFACE is?

例如,當您在源代碼中引入靜態類時,csc編譯器將其轉換為密封的抽象類(如果我錯了,請糾正我)。

但接口怎么樣; CLR知道接口是什么嗎? 或編譯器將其轉換為某種類型的聲明?

例如,當您在源代碼中引入一個抽象類時,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 { } 

你錯了,它是轉換為等效的sealed abstract classstatic 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為例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM