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

Creating Instance of Unknown Generic Type

P: n/a
I want to be able to use a generic type, bind its generic parameters to
specific type(s) and then hold newly created instances in a list of generic
ypes.

Sort of like this :

class Agent<T>
{
...
}

Type t = typeof(Agent<>);

Type[] types {typeof(int)};

t.BindGenericParameters(types);

Agent<> instance = Activator.CreateInstance(t);

List<Agent<>> agents = new List<Agent<>>();

agents.Add(instance);.

This will not compile yet, of course, that is why I am asking :-)

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Joanna,

Can you use a base (abstract) class? Or an interface?

abstract class AgentBase {...}
class Agent<T> : AgentBase {...}
....
List<AgentBase> = new List<AgentBase>;

HTH,
Alexander Shirshov

"Joanna Carter (TeamB)" <jo*****@nospamforme.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I want to be able to use a generic type, bind its generic parameters to
specific type(s) and then hold newly created instances in a list of
generic
ypes.

Sort of like this :

class Agent<T>
{
...
}

Type t = typeof(Agent<>);

Type[] types {typeof(int)};

t.BindGenericParameters(types);

Agent<> instance = Activator.CreateInstance(t);

List<Agent<>> agents = new List<Agent<>>();

agents.Add(instance);.

This will not compile yet, of course, that is why I am asking :-)

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 16 '05 #2

P: n/a
"Alexander Shirshov" <al*******@omnitalented.com> a écrit dans le message de
news: uf**************@TK2MSFTNGP14.phx.gbl...
Can you use a base (abstract) class? Or an interface?

abstract class AgentBase {...}
class Agent<T> : AgentBase {...}
...
List<AgentBase> = new List<AgentBase>;


That would certainly solve the storage part, but my bigger problem is the
instantiation of parameterised generic types when all I have is the generic
parameter type :

/////////////////////

Type t = typeof(Agent<>);

Type[] types {typeof(int)};

t.BindGenericParameters(types);

AgentBase instance = Activator.CreateInstance(t);

/////////////////////

Many thanks

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #3

P: n/a
Type t = typeof(Agent<>);

Type[] types = new Type[]{typeof(int)};

Type genericType = t.MakeGenericType(types);

AgentBase instance = (AgentBase)Activator.CreateInstance(genericType);

HTH,
Alexander

"Joanna Carter (TeamB)" <jo*****@nospamforme.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
"Alexander Shirshov" <al*******@omnitalented.com> a écrit dans le message
de
news: uf**************@TK2MSFTNGP14.phx.gbl...
Can you use a base (abstract) class? Or an interface?

abstract class AgentBase {...}
class Agent<T> : AgentBase {...}
...
List<AgentBase> = new List<AgentBase>;


That would certainly solve the storage part, but my bigger problem is the
instantiation of parameterised generic types when all I have is the
generic
parameter type :

/////////////////////

Type t = typeof(Agent<>);

Type[] types {typeof(int)};

t.BindGenericParameters(types);

AgentBase instance = Activator.CreateInstance(t);

/////////////////////

Many thanks

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 16 '05 #4

P: n/a
"Alexander Shirshov" <al*******@omnitalented.com> a écrit dans le message de
news: uL*************@TK2MSFTNGP15.phx.gbl...

Wonderful!! Once I figured out that MakeGenericType is now called
BindGenericParameters, I realised that the only difference between your code
and mine was that I continued to use 't' instead of the type returned from
BindGenericParameters.

So now I have :

Type t = typeof(Agent<>);

Type[] types = new Type[]{typeof(int)};

Type genericType = t.BindGenericParameters(types);

AgentBase instance = (AgentBase)Activator.CreateInstance(genericType);

....and it works perfectly. Many thanks.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #5

P: n/a
Good. Except...

I'm using February 2005 CTP and it says BindGenericParameters is deprecated.
You should use MakeGenericType instead. So be it:

Type = typeof(Agent<>).MakeGenericType(typeof(int));

Alexander

"Joanna Carter (TeamB)" <jo*****@nospamforme.com> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
"Alexander Shirshov" <al*******@omnitalented.com> a écrit dans le message
de
news: uL*************@TK2MSFTNGP15.phx.gbl...

Wonderful!! Once I figured out that MakeGenericType is now called
BindGenericParameters, I realised that the only difference between your
code
and mine was that I continued to use 't' instead of the type returned from
BindGenericParameters.

So now I have :

Type t = typeof(Agent<>);

Type[] types = new Type[]{typeof(int)};

Type genericType = t.BindGenericParameters(types);

AgentBase instance = (AgentBase)Activator.CreateInstance(genericType);

...and it works perfectly. Many thanks.

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 16 '05 #6

P: n/a
"Alexander Shirshov" <al*******@omnitalented.com> a écrit dans le message de
news: u7**************@TK2MSFTNGP10.phx.gbl...
I'm using February 2005 CTP and it says BindGenericParameters is deprecated. You should use MakeGenericType instead. So be it:

Type = typeof(Agent<>).MakeGenericType(typeof(int));


Well, the latest version that I installed with MSDN is not up there yet, but
I will certainly remember to change it when the next full beta is released.

It certainly is a better name for a method that produces a different type
than that which was passed in. Had that been the name, I don't think I would
have had so much trouble over it :-)

Once again, many thanks

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.