簡體   English   中英

哪個是C#聲明相當於delphi“class of”(類的類型)?

[英]which is the C# declaration equivalent of delphi “class of ” (type of class)?

在delphi中,我可以聲明一種類似的類

type
  TFooClass = class of TFoo;
  TFoo=class
  end;

這個聲明的C#等價物是什么?

您可以在C#中獲得的最接近的是Type類型,其中包含有關類型的元數據。

public class A { }
public static int Main(string[] args)
{
  Type b = typeof(A);
}

它不完全一樣。 在Delphi中,“othertype類型”本身就是一種可以分配給變量的類型。 在C#中,“othertype類型”是一個System.Type實例,可以分配給System.Type類型的任何變量。

例如,在Delphi中,您可以這樣做:

type
  TAClass = class of TA;
  TA = class
  public
    class procedure DoSomething;
  end;

var x : TAClass;
begin
  x := TA;
  x.DoSomething();
end;

你不能在C#中做這樣的事情; 您不能從恰好包含typeof(A)Type實例調用類型A的靜態方法,也不能定義只能包含 typeof(A)或派生類型的變量。

(Delphi元類類型的一些特定模式可以使用泛型來完成:

public class A { }
public class ListOfA<T> where T: A { } 

在這種情況下,T是“A的類型”或A用於構造類的任何派生類。)

暫無
暫無

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

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