private static String NonQualifiedTypeName(this Type type) { RuntimeType runtimeType = type as RuntimeType; if (runtimeType == null) return null; if (runtimeType.HasElementType) { String elementTypeName = runtimeType.InternalRuntimeElementType.NonQualifiedTypeName(); if (elementTypeName == null) return null; String suffix; if (runtimeType.IsArray) { int rank = runtimeType.GetArrayRank(); if (rank == 1) suffix = "[" + (runtimeType.InternalIsMultiDimArray ? "*" : "") + "]"; else suffix = "[" + new String(',', rank - 1) + "]"; } else if (runtimeType.IsByRef) suffix = "&"; else if (runtimeType.IsPointer) suffix = "*"; else return null; return elementTypeName + suffix; } else if (runtimeType.IsGenericParameter) { return null; } else if (runtimeType.IsConstructedGenericType) { StringBuilder sb = new StringBuilder(); String genericTypeDefinitionTypeName = runtimeType.GetGenericTypeDefinition().NonQualifiedTypeName(); if (genericTypeDefinitionTypeName == null) return null; sb.Append(genericTypeDefinitionTypeName); sb.Append("["); String sep = ""; foreach (RuntimeType ga in runtimeType.InternalRuntimeGenericTypeArguments) { String gaTypeName = ga.AssemblyQualifiedTypeName(); if (gaTypeName == null) return null; sb.Append(sep + "[" + gaTypeName + "]"); sep = ","; } sb.Append("]"); return sb.ToString(); } else { RuntimeNamedTypeInfo runtimeNamedTypeInfo = type.GetTypeInfo() as RuntimeNamedTypeInfo; if (runtimeNamedTypeInfo == null) return null; MetadataReader reader = runtimeNamedTypeInfo.Reader; String s = ""; TypeDefinitionHandle typeDefinitionHandle = runtimeNamedTypeInfo.TypeDefinitionHandle; NamespaceDefinitionHandle namespaceDefinitionHandle; do { TypeDefinition typeDefinition = typeDefinitionHandle.GetTypeDefinition(reader); String name = typeDefinition.Name.GetString(reader); if (s == "") s = name; else s = name + "+" + s; namespaceDefinitionHandle = typeDefinition.NamespaceDefinition; typeDefinitionHandle = typeDefinition.EnclosingType; } while (!typeDefinitionHandle.IsNull(reader)); NamespaceChain namespaceChain = new NamespaceChain(reader, namespaceDefinitionHandle); String ns = namespaceChain.NameSpace; if (ns != null) s = ns + "." + s; return s; } }
private static String NonQualifiedTypeName(this Type type) { RuntimeType runtimeType = type as RuntimeType; if (runtimeType == null) { return(null); } if (runtimeType.HasElementType) { String elementTypeName = runtimeType.InternalRuntimeElementType.NonQualifiedTypeName(); if (elementTypeName == null) { return(null); } String suffix; if (runtimeType.IsArray) { int rank = runtimeType.GetArrayRank(); if (rank == 1) { suffix = "[" + (runtimeType.InternalIsMultiDimArray ? "*" : "") + "]"; } else { suffix = "[" + new String(',', rank - 1) + "]"; } } else if (runtimeType.IsByRef) { suffix = "&"; } else if (runtimeType.IsPointer) { suffix = "*"; } else { return(null); } return(elementTypeName + suffix); } else if (runtimeType.IsGenericParameter) { return(null); } else if (runtimeType.IsConstructedGenericType) { StringBuilder sb = new StringBuilder(); String genericTypeDefinitionTypeName = runtimeType.GetGenericTypeDefinition().NonQualifiedTypeName(); if (genericTypeDefinitionTypeName == null) { return(null); } sb.Append(genericTypeDefinitionTypeName); sb.Append("["); String sep = ""; foreach (RuntimeType ga in runtimeType.InternalRuntimeGenericTypeArguments) { String gaTypeName = ga.AssemblyQualifiedTypeName(); if (gaTypeName == null) { return(null); } sb.Append(sep + "[" + gaTypeName + "]"); sep = ","; } sb.Append("]"); return(sb.ToString()); } else { RuntimeNamedTypeInfo runtimeNamedTypeInfo = type.GetTypeInfo() as RuntimeNamedTypeInfo; if (runtimeNamedTypeInfo == null) { return(null); } MetadataReader reader = runtimeNamedTypeInfo.Reader; String s = ""; TypeDefinitionHandle typeDefinitionHandle = runtimeNamedTypeInfo.TypeDefinitionHandle; NamespaceDefinitionHandle namespaceDefinitionHandle; do { TypeDefinition typeDefinition = typeDefinitionHandle.GetTypeDefinition(reader); String name = typeDefinition.Name.GetString(reader); if (s == "") { s = name; } else { s = name + "+" + s; } namespaceDefinitionHandle = typeDefinition.NamespaceDefinition; typeDefinitionHandle = typeDefinition.EnclosingType; }while (!typeDefinitionHandle.IsNull(reader)); NamespaceChain namespaceChain = new NamespaceChain(reader, namespaceDefinitionHandle); String ns = namespaceChain.NameSpace; if (ns != null) { s = ns + "." + s; } return(s); } }