суббота, 9 февраля 2013 г.

Creating generic type object dynamically using reflection



Type type = typeof(MessageProcessor<>).MakeGenericType(key);
That's the best you can do, however without actually knowing what type it is, there's really not much more you can do with it.
EDIT: I should clarify. I changed from var type to Type type. My point is, now you can do something like this:
object obj = Activator.CreateInstance(type);
obj will now be the correct type, but since you don't know what type "key" is at compile time, there's no way to cast it and do anything useful with it.

Комментариев нет: