Create object by class name

  • Thread starter Thread starter MuZZY
  • Start date Start date
M

MuZZY

Hi,

I am comparably new to C# programming, so my apology if the question is
naive :)

In my C# Win Application i have a bunch of classes inherited from a
TBaseObj class, say TRectObj, TCircleObj, etc.

I want to be able to create an instance of such a class by its name:
Something like:
TBaseObj new_obj = CreateObj("TRectObj"), where CreateObj should be
something returning an object instance

I was looking into Activator cass, but it neets an assembly as a
parameter, but i have those classes within my programm, not as a
separate library.

Any ideas would be highly appreciated!

Andrey
 
MuZZY said:
In my C# Win Application i have a bunch of classes inherited from a
TBaseObj class, say TRectObj, TCircleObj, etc.

I want to be able to create an instance of such a class by its name:
Something like:
TBaseObj new_obj = CreateObj("TRectObj"), where CreateObj should be
something returning an object instance

I was looking into Activator cass, but it neets an assembly as a
parameter, but i have those classes within my programm, not as a separate
library.

Hi Andew,

Activator.CreateInstance() also accepts a Type reference and by calling the
static Type.GetType(string name) you can get a reference to your type:

Type t = Type.GetType("TRectObj");
TBaseObj new_obj = (TBaseObj) Activator.CreateInstance(t);

Cheers

Arne Janning
 
Arne said:
Hi Andew,

Activator.CreateInstance() also accepts a Type reference and by calling the
static Type.GetType(string name) you can get a reference to your type:

Type t = Type.GetType("TRectObj");
TBaseObj new_obj = (TBaseObj) Activator.CreateInstance(t);

Cheers

Arne Janning

Thanks Arne!
It helped.
But now i have another problem -
say in base class and in all inheriting classes i have
a static function: "DrawShape()"
I was trying to call it this way:

Type t = Type.GetType(<typename>);
((TBaseObj)t).DrawShape();

Compiler gives an error - can't convert 'Type' into 'TBaseObj'
Is theer a workaround? I'd like to keep the method DrawShape static, and
be able to call it like this.

I guess that 't' should be somehow converted to a class?

Any ideas are highly appreciated!!!

Andrey
 
Arne said:
Hi Andew,

Activator.CreateInstance() also accepts a Type reference and by calling the
static Type.GetType(string name) you can get a reference to your type:

Type t = Type.GetType("TRectObj");
TBaseObj new_obj = (TBaseObj) Activator.CreateInstance(t);

Cheers

Arne Janning

Ok, it works well.
But... say i have a parameterless constructor for my base class TBaseObj
but when i create an instance of a child class TRectObj with no
parametes specified, like in your example, i get an error saying that
no constructor found for TRectObj (it has couple condtructors with
parameters). Doesn't it inherit a parameterless constructor from the
base class?

Thank you,
Andrey
 
Andrey,

Constructors are not inherited. You have to do something along the lines
of:

public class foo : bar {

public foo() : base () {
}

}

If you have no constructors defined, a default parameterless constructor is
inserted into the assembly for you (although I believe this does not
automatically call any base constructor -- check the language spec to be
sure). Once you start defining constructors, though, if you want a
parameterless constructor you have to put one in there.

--Bob
 
Back
Top