Creating an object depending on a value of a parameter

  • Thread starter Thread starter Chamith
  • Start date Start date
C

Chamith

Hi all,
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith
 
Chamith said:
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Look at the docs for Activator.CreateInstance. Sample code:

using System;

class Test
{
static void Main()
{
string typeName = "Foo";

Type t = Type.GetType (typeName);
object o = Activator.CreateInstance(t);
}
}

class Foo
{
public Foo()
{
Console.WriteLine ("Constructing instance of Foo");
}
}
 
If the type to be created is in the same assembly as the creating function then

object CreateIt(string typename)
{
Type t = Type.GetType(typename);
object o = Activator.CreateInstance(t);
return o;
}

if the type isn't in the same assembly (or in mscorlib) then you haven't got enough information to create the type

Regards

Richard Belwett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi all,
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith
 
Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating, i
need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there a
way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith
 
Use a common base class or interface and return that from the method

MyBase CreateIt(string typename)
{
object o = Activator.CreateInstance(Type.GetType(typename));
return (MyBase)o;
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating, i
need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there a
way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith
 
Hi,
As Dino said, use any "good-old" polymorphic technique like Interface
or BaseClass.
 
Back
Top