public static Type GetTypeFromQualifiedName(string assemblyQualifiedName) { int index = TypeNameIndex(assemblyQualifiedName); var typeName = assemblyQualifiedName.Substring(0, index); string assemblyName = assemblyQualifiedName.Substring(index + 2); return(AssemblyInfoCollection.TryCreate(assemblyName).Assembly.GetType(typeName)); }
public static Type RecoveryQualifiedName(string simplifyQualifiedName) { Type type; lock (simplifyQualifiedName) { if (!RecoverySimplifyQualifiedCache.TryGetValue(simplifyQualifiedName, out type)) { StringBuilder builder = new StringBuilder(); RecoveryQualifiedName(simplifyQualifiedName, ref builder); type = AssemblyInfoCollection.GetTypeFromQualifiedName(builder.ToString()); if (type == null) { throw new ArgumentException("无法找到简化限定符的类型 " + simplifyQualifiedName); } RecoverySimplifyQualifiedCache.Add(simplifyQualifiedName, type); } } return(type); }
private static void RecoveryQualifiedName(string simplifyQualifiedName, ref StringBuilder builder) { int builderIndex = builder.Length; int depth = 0; int index = 0; int firstIndex = -1; string assemblyName = string.Empty; for (int i = 0; i < simplifyQualifiedName.Length;) { var c = simplifyQualifiedName[i]; switch (c) { case '[': if (firstIndex == -1) { firstIndex = i; } if (depth == 0) { builder.Append('['); index = i + 1; } depth++; break; case ']': depth--; if (depth == 1) { string str = simplifyQualifiedName.Substring(index + 1, i - index - 1); builder.Append('['); //builder.Append(); RecoveryQualifiedName(str, ref builder); index = i + 2; } if (depth < 2) { builder.Append(']'); } break; case ',': if (firstIndex == -1) { firstIndex = i; } if (depth == 0) { assemblyName = simplifyQualifiedName.Substring(i + 1); i = simplifyQualifiedName.Length; } else if (depth < 2) { builder.Append(','); } break; default: if (depth == 0) { builder.Append(c); } break; } i++; } if (string.IsNullOrEmpty(assemblyName)) { assemblyName = "mscorlib"; } string typeName = firstIndex == -1 ? simplifyQualifiedName : simplifyQualifiedName.Substring(0, firstIndex); var nsIndex = typeName.LastIndexOf('.'); if (nsIndex > -1) { var ns = typeName.Substring(0, nsIndex); builder.Remove(builderIndex, nsIndex); builder.Insert(builderIndex, GetNamespace(ns)); } builder.Append(", "); builder.Append(AssemblyInfoCollection.Find(assemblyName).Fullname); // return builder.ToString(); }