public static ITypeReference CreateClrNamespaceTypeReference(string @namespace, string localName) { if (@namespace == null) { return(new UnknownType(null, localName)); } int assemblyNameIndex = @namespace.IndexOf(";assembly=", StringComparison.OrdinalIgnoreCase); IAssemblyReference asm = DefaultAssemblyReference.CurrentAssembly; if (assemblyNameIndex > -1) { asm = new DefaultAssemblyReference(@namespace.Substring(assemblyNameIndex + ";assembly=".Length)); @namespace = @namespace.Substring(0, assemblyNameIndex); } return(new GetClassTypeReference(asm, @namespace, localName, 0)); }
static ITypeReference CreateGetClassTypeReference(string assemblyName, string typeName, int tpc) { IModuleReference assemblyReference; if (assemblyName != null) { assemblyReference = new DefaultAssemblyReference(assemblyName); } else { assemblyReference = null; } int pos = typeName.LastIndexOf('.'); if (pos < 0) { return(new GetClassTypeReference(assemblyReference, string.Empty, typeName, tpc)); } else { return(new GetClassTypeReference(assemblyReference, typeName.Substring(0, pos), typeName.Substring(pos + 1), tpc)); } }
/// <summary> /// Creates a reference to the specified type. /// </summary> /// <param name="type">The type to be converted.</param> /// <returns>Returns the type reference.</returns> /// <remarks> /// If the type is open (contains type parameters '`0' or '``0'), /// an <see cref="ITypeResolveContext"/> with the appropriate CurrentTypeDefinition/CurrentMember is required /// to resolve the type reference. /// For closed types, the root type resolve context for the compilation is sufficient. /// </remarks> public static ITypeReference ToTypeReference(this Type type) { if (type == null) { return(SpecialType.UnknownType); } if (type.IsGenericType && !type.IsGenericTypeDefinition) { ITypeReference def = ToTypeReference(type.GetGenericTypeDefinition()); Type[] arguments = type.GetGenericArguments(); ITypeReference[] args = new ITypeReference[arguments.Length]; bool allUnbound = true; for (int i = 0; i < arguments.Length; i++) { args[i] = ToTypeReference(arguments[i]); allUnbound &= args[i].Equals(SpecialType.UnboundTypeArgument); } if (allUnbound) { return(def); } else { return(new ParameterizedTypeReference(def, args)); } } else if (type.IsArray) { return(new ArrayTypeReference(ToTypeReference(type.GetElementType()), type.GetArrayRank())); } else if (type.IsPointer) { return(new PointerTypeReference(ToTypeReference(type.GetElementType()))); } else if (type.IsByRef) { return(new ByReferenceTypeReference(ToTypeReference(type.GetElementType()))); } else if (type.IsGenericParameter) { if (type.DeclaringMethod != null) { return(TypeParameterReference.Create(SymbolKind.Method, type.GenericParameterPosition)); } else { return(TypeParameterReference.Create(SymbolKind.TypeDefinition, type.GenericParameterPosition)); } } else if (type.DeclaringType != null) { if (type == typeof(Dynamic)) { return(SpecialType.Dynamic); } else if (type == typeof(Null)) { return(SpecialType.NullType); } else if (type == typeof(UnboundTypeArgument)) { return(SpecialType.UnboundTypeArgument); } ITypeReference baseTypeRef = ToTypeReference(type.DeclaringType); int typeParameterCount; string name = SplitTypeParameterCountFromReflectionName(type.Name, out typeParameterCount); return(new NestedTypeReference(baseTypeRef, name, typeParameterCount)); } else { IModuleReference assemblyReference = new DefaultAssemblyReference(type.Assembly.FullName); int typeParameterCount; string name = SplitTypeParameterCountFromReflectionName(type.Name, out typeParameterCount); return(new GetClassTypeReference(assemblyReference, type.Namespace, name, typeParameterCount)); } }