469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Creating Instance of Unknown Generic Type

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
6 25352
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
"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
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
"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
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
"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.

Similar topics

4 posts views Thread by Cool Guy | last post: by
8 posts views Thread by Cool Guy | last post: by
4 posts views Thread by Charles Churchill | last post: by
5 posts views Thread by vtjumper | last post: by
3 posts views Thread by webcliff | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.