/// <summary> /// Strip off *, &, and []. /// </summary> private static ITypeReference UnwrapTypeReference(ITypeReference typeReference, EmitContext context) { while (true) { IArrayTypeReference arrType = typeReference as IArrayTypeReference; if (arrType != null) { typeReference = arrType.GetElementType(context); continue; } IPointerTypeReference pointer = typeReference as IPointerTypeReference; if (pointer != null) { typeReference = pointer.GetTargetType(context); continue; } IManagedPointerTypeReference reference = typeReference as IManagedPointerTypeReference; if (reference != null) { typeReference = reference.GetTargetType(context); continue; } return(typeReference); } }
internal static string GetSerializedTypeName(this ITypeReference typeReference, EmitContext context, ref bool isAssemblyQualified) { var pooled = PooledStringBuilder.GetInstance(); StringBuilder sb = pooled.Builder; IArrayTypeReference arrType = typeReference as IArrayTypeReference; if (arrType != null) { typeReference = arrType.GetElementType(context); bool isAssemQual = false; AppendSerializedTypeName(sb, typeReference, ref isAssemQual, context); if (arrType.IsVector) { sb.Append("[]"); } else { sb.Append('['); if (arrType.Rank == 1) { sb.Append('*'); } sb.Append(',', (int)arrType.Rank - 1); sb.Append(']'); } goto done; } IPointerTypeReference pointer = typeReference as IPointerTypeReference; if (pointer != null) { typeReference = pointer.GetTargetType(context); bool isAssemQual = false; AppendSerializedTypeName(sb, typeReference, ref isAssemQual, context); sb.Append('*'); goto done; } IManagedPointerTypeReference reference = typeReference as IManagedPointerTypeReference; if (reference != null) { typeReference = reference.GetTargetType(context); bool isAssemQual = false; AppendSerializedTypeName(sb, typeReference, ref isAssemQual, context); sb.Append('&'); goto done; } INamespaceTypeReference namespaceType = typeReference.AsNamespaceTypeReference; if (namespaceType != null) { if (namespaceType.NamespaceName.Length != 0) { sb.Append(namespaceType.NamespaceName); sb.Append('.'); } sb.Append(GetMangledAndEscapedName(namespaceType)); goto done; } if (typeReference.IsTypeSpecification()) { ITypeReference uninstantiatedTypeReference = typeReference.GetUninstantiatedGenericType(); ArrayBuilder <ITypeReference> consolidatedTypeArguments = ArrayBuilder <ITypeReference> .GetInstance(); typeReference.GetConsolidatedTypeArguments(consolidatedTypeArguments, context); bool uninstantiatedTypeIsAssemblyQualified = false; sb.Append(GetSerializedTypeName(uninstantiatedTypeReference, context, ref uninstantiatedTypeIsAssemblyQualified)); sb.Append('['); bool first = true; foreach (ITypeReference argument in consolidatedTypeArguments) { if (first) { first = false; } else { sb.Append(','); } bool isAssemQual = true; AppendSerializedTypeName(sb, argument, ref isAssemQual, context); } consolidatedTypeArguments.Free(); sb.Append(']'); goto done; } INestedTypeReference nestedType = typeReference.AsNestedTypeReference; if (nestedType != null) { bool nestedTypeIsAssemblyQualified = false; sb.Append(GetSerializedTypeName(nestedType.GetContainingType(context), context, ref nestedTypeIsAssemblyQualified)); sb.Append('+'); sb.Append(GetMangledAndEscapedName(nestedType)); goto done; } // TODO: error done: if (isAssemblyQualified) { AppendAssemblyQualifierIfNecessary(sb, UnwrapTypeReference(typeReference, context), out isAssemblyQualified, context); } return(pooled.ToStringAndFree()); }
private static void AppendAssemblyQualifierIfNecessary(StringBuilder sb, ITypeReference typeReference, out bool isAssemQualified, EmitContext context) { INestedTypeReference nestedType = typeReference.AsNestedTypeReference; if (nestedType != null) { AppendAssemblyQualifierIfNecessary(sb, nestedType.GetContainingType(context), out isAssemQualified, context); return; } IGenericTypeInstanceReference genInst = typeReference.AsGenericTypeInstanceReference; if (genInst != null) { AppendAssemblyQualifierIfNecessary(sb, genInst.GetGenericType(context), out isAssemQualified, context); return; } IArrayTypeReference arrType = typeReference as IArrayTypeReference; if (arrType != null) { AppendAssemblyQualifierIfNecessary(sb, arrType.GetElementType(context), out isAssemQualified, context); return; } IPointerTypeReference pointer = typeReference as IPointerTypeReference; if (pointer != null) { AppendAssemblyQualifierIfNecessary(sb, pointer.GetTargetType(context), out isAssemQualified, context); return; } IManagedPointerTypeReference reference = typeReference as IManagedPointerTypeReference; if (reference != null) { AppendAssemblyQualifierIfNecessary(sb, reference.GetTargetType(context), out isAssemQualified, context); return; } isAssemQualified = false; IAssemblyReference referencedAssembly = null; INamespaceTypeReference namespaceType = typeReference.AsNamespaceTypeReference; if (namespaceType != null) { referencedAssembly = namespaceType.GetUnit(context) as IAssemblyReference; } if (referencedAssembly != null) { var containingAssembly = context.Module.GetContainingAssembly(context); if (containingAssembly == null || !ReferenceEquals(referencedAssembly, containingAssembly)) { sb.Append(", "); sb.Append(MetadataWriter.StrongName(referencedAssembly)); isAssemQualified = true; } } }