c# 反射获取一个类型取到null

经常用 GetType 获取一个Type

1. 想获取的 Type 在当前执行的 assembly 里面,则可以直接获取

Type myType1 = Type.GetType("System.Int32");

2. 想获取的 Type 不在当前的assembly 里面,直接获取得到 null

Type t = Type.GetType("System.Runtime.InteropServices.RuntimeInformation");
// t == null

必须用 AssemblyQualifiedName 去获取

Type t = Type.GetType("System.Runtime.InteropServices.RuntimeInformation, System.Runtime.InteropServices.RuntimeInformation, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

猜你喜欢

转载自www.cnblogs.com/chenyingzuo/p/12366016.html
今日推荐