有没有办法从C#中的引用类初始化对象?

时间:2015-07-08 06:36:04

标签: c# class delphi reference

在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#中找不到类似的东西。

1 个答案:

答案 0 :(得分:3)

像这样使用Type

public class TShape { }

Type t = typeof(TShape);

要通过t变量初始化对象,请使用Activator.CreateInstance(t)

Shape shp = (Shape)Activator.CreateInstance(t);