Type.GetType(“namespace.a.b.ClassName”) returns null

前端 未结 16 1365
旧巷少年郎
旧巷少年郎 2020-11-22 02:58

This code:

Type.GetType(\"namespace.a.b.ClassName\")

returns null.

and I have in the usings:

using nam         


        
16条回答
  •  旧时难觅i
    2020-11-22 03:46

    Dictionary typeCache;
    ...
    public static bool TryFindType(string typeName, out Type t) {
        lock (typeCache) {
            if (!typeCache.TryGetValue(typeName, out t)) {
                foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
                    t = a.GetType(typeName);
                    if (t != null)
                        break;
                }
                typeCache[typeName] = t; // perhaps null
            }
        }
        return t != null;
    }
    

提交回复
热议问题