'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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top