在delphi中我可以声明一个 类的类型 像这样
type
TFooClass = class of TFoo;
TFoo=class
end;
这个声明的C#等价物是什么?
在delphi中我可以声明一个 类的类型 像这样
type
TFooClass = class of TFoo;
TFoo=class
end;
这个声明的C#等价物是什么?
你可以在C#中得到的最接近的是 Type
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#中做这样的事情;你不能从实例中调用类型A的静态方法 Type
碰巧举行 typeof(A)
,你也不能定义一个可以的变量 只要 保持 typeof(A)
或派生类型。
(Delphi元类类型的一些特定模式可以使用泛型来完成:
public class A { }
public class ListOfA<T> where T: A { }
在这种情况下,T是“A的类型”或A用于构造类的任何派生类。)
你可以在C#中得到的最接近的是 Type
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#中做这样的事情;你不能从实例中调用类型A的静态方法 Type
碰巧举行 typeof(A)
,你也不能定义一个可以的变量 只要 保持 typeof(A)
或派生类型。
(Delphi元类类型的一些特定模式可以使用泛型来完成:
public class A { }
public class ListOfA<T> where T: A { }
在这种情况下,T是“A的类型”或A用于构造类的任何派生类。)