471,599 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

Create Generic Collection at Runtime

Is there anyway to do the following?

Type myType = typeof(User);
Collection<myType> list = new Collection<myType>();

I know I could just use User instead of myType but have a function that
takes a type and populates a collection then calls SetValue using
reflection. How would I create the generic collection to fill it and call
SetValue?

thanks
Paul
Nov 17 '05 #1
7 9022
template is called "compile-time" polymorphism, so the <myType> should
been set at "COMPILE-TIME", you can't use runtime type instead.

my suggestion is use Collection<Object>, and convert it, or just create
an Interface for your <myType>s to implement. and use
Conllection<myTypeInterface> instead.

Nov 17 '05 #2

"Paul Welter" <pw*****@loresoft.com> wrote in message
news:Oa*************@TK2MSFTNGP15.phx.gbl...
Is there anyway to do the following?

Type myType = typeof(User);
Collection<myType> list = new Collection<myType>();

I know I could just use User instead of myType but have a function that
takes a type and populates a collection then calls SetValue using
reflection. How would I create the generic collection to fill it and call
SetValue?

Why not have the function take a type parameter instead of passing it a
type?

Collection<T> CreateCollection<T>(T[] dataItems)
{
}

or what have you.
Nov 17 '05 #3
Paul Welter wrote:
Is there anyway to do the following?

Type myType = typeof(User);
Collection<myType> list = new Collection<myType>();

I know I could just use User instead of myType but have a function that
takes a type and populates a collection then calls SetValue using
reflection. How would I create the generic collection to fill it and call
SetValue?


You can do this:

Type genericType = typeof(Collection<>);
Type constructedType = genericType.MakeGenericType(myType);

Now you can create an instance of the constructed type and call methods
on it via Reflection:

object myObject = Activator.CreateInstance(constructedType, ...);
myObject.GetType().InvokeMember("SetValue", BindingFlags.Instance |
BindingFlags.InvokeMethod | BindingFlags.Public,
null, myObject, new object[] { ... });

Now finally, let me say this: I'm quite sure there should be a better
way to do whatever it is exactly that you want to do. Especially since
the advent of Generics... Think about it, or tell us about it.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4
Type.MakeGenericType works great, thanks. Couple question about
MakeGenericType though. Is there a big performance hit for using
Type.MakeGenericType? Is the performance hit in calling
Type.MakeGenericType or when calling
Activator.CreateInstance(constructedType, ...)? Would something be gained
if I cached the Type output from the call to Type.MakeGenericType? I was
thinking something like this ...

private static Hashtable genericClassTypeCache = Hashtable.Synchronized(new
Hashtable());

private Type GetGenericClassType(Type classType, Type genericType)
{
string key = classType.ToString() + genericType.ToString();
if (genericClassTypeCache.Contains(key))
{
return (Type)genericClassTypeCache[key];
}

Type constructedType = classType.MakeGenericType(genericType);
genericClassTypeCache.Add(key, constructedType);

return constructedType;
}

thanks
Paul

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:u9****************@TK2MSFTNGP10.phx.gbl...
Paul Welter wrote:
Is there anyway to do the following?

Type myType = typeof(User);
Collection<myType> list = new Collection<myType>();

I know I could just use User instead of myType but have a function that
takes a type and populates a collection then calls SetValue using
reflection. How would I create the generic collection to fill it and
call SetValue?


You can do this:

Type genericType = typeof(Collection<>);
Type constructedType = genericType.MakeGenericType(myType);

Now you can create an instance of the constructed type and call methods on
it via Reflection:

object myObject = Activator.CreateInstance(constructedType, ...);
myObject.GetType().InvokeMember("SetValue", BindingFlags.Instance |
BindingFlags.InvokeMethod | BindingFlags.Public,
null, myObject, new object[] { ... });

Now finally, let me say this: I'm quite sure there should be a better way
to do whatever it is exactly that you want to do. Especially since the
advent of Generics... Think about it, or tell us about it.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #5
learned a lot, thanks

Nov 17 '05 #6
Well, I'll answer this one myself. I did some simple performance testing
and found that there was no difference between caching the type and just
calling MakeGenericType every time. In the test I used 3 different generic
collection types, 8 classes to use as the generic and ran it for about 200
reps. The average time for calling MakeGenericType every time was 0.0156125
milliseconds. The average time when using the caching function below was
0.018180451 milliseconds. I guess the runtime is doing its own caching, no
need for it in this case. I would have thought there was going to be a
bigger hit then that as generics are optimized at compile time.

thanks
~ Paul

"Paul Welter" <pw*****@loresoft.com> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
Type.MakeGenericType works great, thanks. Couple question about
MakeGenericType though. Is there a big performance hit for using
Type.MakeGenericType? Is the performance hit in calling
Type.MakeGenericType or when calling
Activator.CreateInstance(constructedType, ...)? Would something be gained
if I cached the Type output from the call to Type.MakeGenericType? I was
thinking something like this ...

private static Hashtable genericClassTypeCache =
Hashtable.Synchronized(new Hashtable());

private Type GetGenericClassType(Type classType, Type genericType)
{
string key = classType.ToString() + genericType.ToString();
if (genericClassTypeCache.Contains(key))
{
return (Type)genericClassTypeCache[key];
}

Type constructedType = classType.MakeGenericType(genericType);
genericClassTypeCache.Add(key, constructedType);

return constructedType;
}

Nov 17 '05 #7
Paul Welter wrote:
Well, I'll answer this one myself. I did some simple performance testing
and found that there was no difference between caching the type and just
calling MakeGenericType every time. In the test I used 3 different generic
collection types, 8 classes to use as the generic and ran it for about 200
reps. The average time for calling MakeGenericType every time was 0.0156125
milliseconds. The average time when using the caching function below was
0.018180451 milliseconds. I guess the runtime is doing its own caching, no
need for it in this case. I would have thought there was going to be a
bigger hit then that as generics are optimized at compile time.


I guess you will see a performance hit comparing this approach with the
construction of the object without Reflection. In .NET 2.0, enormous
improvements have been made to Reflection performance-wise, but it's
still slower by a huge factor.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Steven Cummings | last post: by
19 posts views Thread by Brett Romero | last post: by
5 posts views Thread by Michi Henning | last post: by
6 posts views Thread by Venkatesh Bhupathi | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.