Type.MakeGeneri cType works great, thanks. Couple question about
MakeGenericType though. Is there a big performance hit for using
Type.MakeGeneri cType? Is the performance hit in calling
Type.MakeGeneri cType or when calling
Activator.Creat eInstance(const ructedType, ...)? Would something be gained
if I cached the Type output from the call to Type.MakeGeneri cType? I was
thinking something like this ...
private static Hashtable genericClassTyp eCache = Hashtable.Synch ronized(new
Hashtable());
private Type GetGenericClass Type(Type classType, Type genericType)
{
string key = classType.ToStr ing() + genericType.ToS tring();
if (genericClassTy peCache.Contain s(key))
{
return (Type)genericCl assTypeCache[key];
}
Type constructedType = classType.MakeG enericType(gene ricType);
genericClassTyp eCache.Add(key, constructedType );
return constructedType ;
}
thanks
Paul
"Oliver Sturm" <ol****@sturmne t.org> wrote in message
news:u9******** ********@TK2MSF TNGP10.phx.gbl. ..
Paul Welter wrote: Is there anyway to do the following?
Type myType = typeof(User);
Collection<myTy pe> list = new Collection<myTy pe>();
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(Collecti on<>);
Type constructedType = genericType.Mak eGenericType(my Type);
Now you can create an instance of the constructed type and call methods on
it via Reflection:
object myObject = Activator.Creat eInstance(const ructedType, ...);
myObject.GetTyp e().InvokeMembe r("SetValue", BindingFlags.In stance |
BindingFlags.In vokeMethod | BindingFlags.Pu blic,
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