Pass An Instantiated System.Type as a Type Parameter for a Generic Class

問題:

The title is kind of obscure.標題有點晦澀。 What I want to know is if this is possible:我想知道的是這是否可能:

string typeName = <read type name from somwhere>;
Type myType = Type.GetType(typeName);

MyGenericClass<myType> myGenericClass = new MyGenericClass<myType>();

Obviously, MyGenericClass is described as:顯然,MyGenericClass 被描述爲:

public class MyGenericClass<T>

Right now, the compiler complains that 'The type or namespace 'myType' could not be found." There has got to be a way to do this.現在,編譯器抱怨“找不到類型或命名空間‘myType’。”必須有辦法做到這一點。


解決方案:

參考一: https://en.stackoom.com/question/17EB
參考二: https://stackoom.com/question/17EB
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章