tags:

views:

249

answers:

1

Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround?

Type k = typeof(double);
List<k> lst = new List<k>();
+17  A: 

Yes, there is:

var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(typeof(double));
var list = Activator.CreateInstance(specificListType);
David M
I think this is what I want. Let me double check and I'll mark yours as the answer momentarily.
Chris
I think the main problem here is that you don't describe what you want to *us*. You show us a failed attempt at *something* and then asks how to accomplish what *you want*. If you want answers, and not guesses, you should describe what you need to do, instead of how you attempted to do it.
Lasse V. Karlsen
This should work, thanks.
Chris
I basically want to create a List<> where the type is specified as a Type variable.
Chris
Yes, I think that was clear (to me, anyway) in your question, so I thought this was what you were after...
David M
Thanks David M, your code does exactly what I needed. Sorry to those who were unable to answer because I didn't provide an adequate question.
Chris