all groups > c# > july 2005 >
You're in the

c#

group:

Create Generic Collection at Runtime


Re: Create Generic Collection at Runtime realfun NO[at]SPAM gmail.com
7/31/2005 8:05:02 PM
c#:
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.
Create Generic Collection at Runtime Paul Welter
7/31/2005 9:56:33 PM
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

Re: Create Generic Collection at Runtime Daniel O'Connell [C# MVP]
7/31/2005 11:21:52 PM

[quoted text, click to view]
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.

Re: Create Generic Collection at Runtime Oliver Sturm
8/1/2005 11:01:09 AM
[quoted text, click to view]

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
Re: Create Generic Collection at Runtime Paul Welter
8/2/2005 3:52:40 PM
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

[quoted text, click to view]

Re: Create Generic Collection at Runtime realfun NO[at]SPAM gmail.com
8/2/2005 6:11:10 PM
learned a lot, thanks
Re: Create Generic Collection at Runtime Paul Welter
8/2/2005 11:08:53 PM
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

[quoted text, click to view]

Re: Create Generic Collection at Runtime Oliver Sturm
8/3/2005 10:18:52 AM
[quoted text, click to view]

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
AddThis Social Bookmark Button