Is there a way to initialize an object from a reference class in C#? -


this question has answer here:

in delphi, when want create object uncertain derived class, using class of statement;

tshape = class public   procedure draw; end;  tcircle = class(tshape) public   procedure draw; end;  tshapeclassref = class of tshape; 

and creating object as;

var   ref:tshapeclassref;   drawing:tshape; begin   ref:=tcircle;   drawing:=ref.create;   drawing.draw; //this circle object, , draws circle end; 

i couldn't find in c#.

use type this:

public class tshape { } 

and:

type t = typeof(tshape); 

to initialize object through t variable, use activator.createinstance(t):

shape shp = (shape)activator.createinstance(t); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -