Get type from generic type fullname

后端 未结 3 1731
执念已碎
执念已碎 2021-01-18 17:27

I would like to get type from generic type fullname like that :

var myType = Type.GetType(\"MyProject.MyGenericType`1[[MyProject.MySimpleType, MyProject, Ver         


        
3条回答
  •  臣服心动
    2021-01-18 18:14

    Try this:

    var myType = Type.GetType("MyProject.MyGenericType`1, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]");
    

    Then use MakeGenericType():

    var finalType = myType.MakeGenericType(Type.GetType("MyProject.MySimpleType"));
    

    Alternatively, if the open generic type can be determined at compile type, you can simply use the typeof operator with <>:

    var myType = typeof(MyProject.MyGenericType<>);
    var finalType = myType.MakeGenericType(typeof(MyProject.MySimpleType));
    

    See MSDN

提交回复
热议问题