'new' help - newbie

  • Thread starter Thread starter Timothy V
  • Start date Start date
T

Timothy V

Hi,
I was wondering how to create an instance of a System.Type variable (if it
is possible). For example:

object someMethod(System.Type st)
{
object o = new st(); // I know this is incorrect
return o;
}

I know the above line is incorrect, but it is the best was i could descibe
the problem.

Thanks very much in advance,

Tim.
 
Timothy,
I was wondering how to create an instance of a System.Type variable (if it
is possible). For example:

object someMethod(System.Type st)
{
object o = new st(); // I know this is incorrect
return o;
}

I know the above line is incorrect, but it is the best was i could descibe
the problem.

One way to do this is to use the "typeof" operator. So, you can substitute
"typeof( System.Int32)" for "new st( )." There are other ways to do the
same thing.

Regards,

Randy
 
Hi Timothy,

The Type class has a GetConstructor method which I believe should enable you to create a new object of the Type. It returns a ConstructorInfo object that has an Invoke method that may do what you want.

Happy coding!
Morten Wennevik [C# MVP]
 
Timothy V said:
I was wondering how to create an instance of a System.Type variable (if it
is possible). For example:

object someMethod(System.Type st)
{
object o = new st(); // I know this is incorrect
return o;
}

I know the above line is incorrect, but it is the best was i could descibe
the problem.

Have a look at Activator.CreateInstance.
 
Back
Top