在Delphi中,当我想从不确定的派生object
创建class
时,我正在使用class of
语句;
TShape = class
public
procedure Draw;
end;
TCircle = class(TShape)
public
procedure Draw;
end;
TShapeClassRef = class of TShape;
我正在创建对象;
var
ref:TShapeClassRef;
drawing:TShape;
begin
ref:=TCircle;
drawing:=ref.Create;
drawing.draw; //this is a circle object, and it draws circle
end;
我在c#中找不到类似的东西。
答案 0 :(得分:3)
像这样使用Type
:
public class TShape { }
和
Type t = typeof(TShape);
要通过t
变量初始化对象,请使用Activator.CreateInstance(t)
:
Shape shp = (Shape)Activator.CreateInstance(t);