Create Generic Collection at Runtime

P

Paul Welter

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
 
R

realfun

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

Daniel O'Connell [C# MVP]

Paul Welter said:
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.
 
O

Oliver Sturm

Paul said:
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
 
P

Paul Welter

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 said:
Paul said:
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
 
P

Paul Welter

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
 
O

Oliver Sturm

Paul said:
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
 

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

Top