By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

Is there any way to pass a type as a parameter?

P: n/a
I'd like to create a method that will instantiate an object of whatever type
is specified as the argument. Is this possible?
This C# code does not work but it shows what I'm trying accomplish. Thanks.
private void NewObject(Type x)
{
x newObject = new x();
}
Mar 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"jacobryce" <ja*******@discussions.microsoft.com> wrote
I'd like to create a method that will instantiate an object of whatever
type
is specified as the argument. Is this possible?
This C# code does not work but it shows what I'm trying accomplish.
Thanks.
private void NewObject(Type x)
{
x newObject = new x();
}


That's pretty close. Just use the System.Activator class to instanciate your
type, instead of the "new" operator.

.... you won't really be able to use strong typing though for your code. To
do this you'll need (want) an Interface definition or Abstract base class
you can use, and bring polymorphism into play...

--
Chris Mullins


Mar 15 '06 #2

P: n/a
Thank you. I've attempted to use System.Activator.CreateInstance(x) but it
errors that the parameter in the caller "is a 'type' but is used like a
'variable'." Any idea how to get around this?

"Chris Mullins" wrote:
"jacobryce" <ja*******@discussions.microsoft.com> wrote
I'd like to create a method that will instantiate an object of whatever
type
is specified as the argument. Is this possible?
This C# code does not work but it shows what I'm trying accomplish.
Thanks.
private void NewObject(Type x)
{
x newObject = new x();
}


That's pretty close. Just use the System.Activator class to instanciate your
type, instead of the "new" operator.

.... you won't really be able to use strong typing though for your code. To
do this you'll need (want) an Interface definition or Abstract base class
you can use, and bring polymorphism into play...

--
Chris Mullins


Mar 15 '06 #3

P: n/a
jacobryce <ja*******@discussions.microsoft.com> wrote:
Thank you. I've attempted to use System.Activator.CreateInstance(x) but it
errors that the parameter in the caller "is a 'type' but is used like a
'variable'." Any idea how to get around this?


Well, you could declare your variable as "object". That would involve
boxing if it's a value type, but that'll happen anyway because
CreateInstance's return type is object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '06 #4

P: n/a
I did declare my instance variable as type Object, if I'm understanding you
correctly. The problem I had was in trying to pass in a Type as a parameter
in the method call:
private void NewObject(Type x)
{
x newObject = new x();
}

NewObject(anyType);


"Jon Skeet [C# MVP]" wrote:
jacobryce <ja*******@discussions.microsoft.com> wrote:
Thank you. I've attempted to use System.Activator.CreateInstance(x) but it
errors that the parameter in the caller "is a 'type' but is used like a
'variable'." Any idea how to get around this?


Well, you could declare your variable as "object". That would involve
boxing if it's a value type, but that'll happen anyway because
CreateInstance's return type is object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 16 '06 #5

P: n/a
"jacobryce" <ja*******@discussions.microsoft.com> wrote:
I did declare my instance variable as type Object, if I'm understanding you
correctly.


I think we're still miscommunicating.

Here's a code sample:

Given the Classes defined this way:
public interface MyInterface
{
string DoSomething(string a, string b);
}

public class ClassOne : MyInterface
{
public string DoSomething(string a, string b)
{
return a + b;
}
}
public class ClassTwo : MyInterface
{
public string DoSomething(string a, string b)
{
return "Hello " + a + "Goodbye " + b;
}
}

I can run the following Code:
private void button1_Click(object sender, EventArgs e)
{
Create(typeof(ClassOne));
}
void Create(Type t)
{
MyInterface i = System.Activator.CreateInstance(t) as MyInterface;
string s = i.DoSomething("test", "test");
}

Hope this helps.

--
Chris Mullins
Mar 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.