Ejemplo n.º 1
0
        public TypeReference ResolveTypeName(string typeNameString)
        {
            if (string.IsNullOrEmpty(typeNameString))
            {
                return(null);
            }

            TypeName parsedTypeName;

            try {
                parsedTypeName = TypeParser.ParseTypeName(typeNameString);
            } catch (ArgumentException) {
                return(null);
            } catch (System.IO.FileLoadException) {
                return(null);
            }

            if (parsedTypeName is AssemblyQualifiedTypeName assemblyQualifiedTypeName)
            {
                return(ResolveTypeName(null, assemblyQualifiedTypeName));
            }

            foreach (var assemblyDefinition in _context.GetReferencedAssemblies())
            {
                var foundType = ResolveTypeName(assemblyDefinition, parsedTypeName);
                if (foundType != null)
                {
                    return(foundType);
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
 public bool TryResolveTypeName(AssemblyDefinition assembly, string typeNameString, [NotNullWhen(true)] out TypeReference?typeReference)
 {
     typeReference = ResolveTypeName(assembly, TypeParser.ParseTypeName(typeNameString));
     return(typeReference != null);
 }
Ejemplo n.º 3
0
        public bool TryResolveTypeName(string typeNameString, ICustomAttributeProvider?origin, [NotNullWhen(true)] out TypeReference?typeReference, [NotNullWhen(true)] out AssemblyDefinition?typeAssembly, bool needsAssemblyName = true)
        {
            typeReference = null;
            typeAssembly  = null;
            if (string.IsNullOrEmpty(typeNameString))
            {
                return(false);
            }

            TypeName parsedTypeName;

            try {
                parsedTypeName = TypeParser.ParseTypeName(typeNameString);
            } catch (ArgumentException) {
                return(false);
            } catch (System.IO.FileLoadException) {
                return(false);
            }

            if (parsedTypeName is AssemblyQualifiedTypeName assemblyQualifiedTypeName)
            {
                typeAssembly = _context.TryResolve(assemblyQualifiedTypeName.AssemblyName.Name);
                if (typeAssembly == null)
                {
                    return(false);
                }

                typeReference = ResolveTypeName(typeAssembly, assemblyQualifiedTypeName.TypeName);
                return(typeReference != null);
            }

            // If parsedTypeName doesn't have an assembly name in it but it does have a namespace,
            // search for the type in the calling object's assembly. If not found, look in the core
            // assembly.
            typeAssembly = origin switch {
                AssemblyDefinition asm => asm,
                TypeDefinition type => type.Module?.Assembly,
                IMemberDefinition member => member.DeclaringType.Module.Assembly,
                                   null => null,
                                   _ => throw new NotSupportedException()
            };

            if (typeAssembly != null && TryResolveTypeName(typeAssembly, parsedTypeName, out typeReference))
            {
                return(true);
            }

            // If type is not found in the caller's assembly, try in core assembly.
            typeAssembly = _context.TryResolve(PlatformAssemblies.CoreLib);
            if (typeAssembly != null && TryResolveTypeName(typeAssembly, parsedTypeName, out typeReference))
            {
                return(true);
            }

            // It is common to use Type.GetType for looking if a type is available.
            // If no type was found only warn and return null.
            if (needsAssemblyName && origin != null)
            {
                _context.LogWarning(new MessageOrigin(origin), DiagnosticId.TypeWasNotFoundInAssemblyNorBaseLibrary, typeNameString);
            }

            typeAssembly = null;
            return(false);

            bool TryResolveTypeName(AssemblyDefinition assemblyDefinition, TypeName typeName, [NotNullWhen(true)] out TypeReference?typeReference)
            {
                typeReference = null;
                if (assemblyDefinition == null)
                {
                    return(false);
                }

                typeReference = ResolveTypeName(assemblyDefinition, typeName);
                return(typeReference != null);
            }
        }
Ejemplo n.º 4
0
 public TypeReference ResolveTypeName(AssemblyDefinition assembly, string typeNameString)
 {
     return(ResolveTypeName(assembly, TypeParser.ParseTypeName(typeNameString)));
 }
Ejemplo n.º 5
0
        public TypeReference ResolveTypeName(string typeNameString, ICustomAttributeProvider origin, out AssemblyDefinition typeAssembly, bool needsAssemblyName = true)
        {
            typeAssembly = null;
            if (string.IsNullOrEmpty(typeNameString))
            {
                return(null);
            }

            TypeName parsedTypeName;

            try {
                parsedTypeName = TypeParser.ParseTypeName(typeNameString);
            } catch (ArgumentException) {
                return(null);
            } catch (System.IO.FileLoadException) {
                return(null);
            }

            if (parsedTypeName is AssemblyQualifiedTypeName assemblyQualifiedTypeName)
            {
                typeAssembly = _context.TryResolve(assemblyQualifiedTypeName.AssemblyName.Name);
                if (typeAssembly == null)
                {
                    return(null);
                }

                return(ResolveTypeName(typeAssembly, assemblyQualifiedTypeName.TypeName));
            }

            // If parsedTypeName doesn't have an assembly name in it but it does have a namespace,
            // search for the type in the calling object's assembly. If not found, look in the core
            // assembly.
            typeAssembly = origin switch {
                AssemblyDefinition asm => asm,
                TypeDefinition type => type.Module?.Assembly,
                IMemberDefinition member => member.DeclaringType.Module.Assembly,
                                   null => null,
                                   _ => throw new NotSupportedException()
            };

            if (typeAssembly != null && TryResolveTypeName(typeAssembly, parsedTypeName, out var typeRef))
            {
                return(typeRef);
            }

            // If type is not found in the caller's assembly, try in core assembly.
            typeAssembly = _context.TryResolve(PlatformAssemblies.CoreLib);
            if (typeAssembly != null && TryResolveTypeName(typeAssembly, parsedTypeName, out var typeRefFromSPCL))
            {
                return(typeRefFromSPCL);
            }

            // It is common to use Type.GetType for looking if a type is available.
            // If no type was found only warn and return null.
            if (needsAssemblyName && origin != null)
            {
                _context.LogWarning($"Type '{typeNameString}' was not found in the caller assembly nor in the base library. " +
                                    $"Type name strings used for dynamically accessing a type should be assembly qualified.",
                                    2105, new MessageOrigin(origin));
            }

            typeAssembly = null;
            return(null);

            bool TryResolveTypeName(AssemblyDefinition assemblyDefinition, TypeName typeName, out TypeReference typeReference)
            {
                typeReference = null;
                if (assemblyDefinition == null)
                {
                    return(false);
                }

                typeReference = ResolveTypeName(assemblyDefinition, typeName);
                return(typeReference != null);
            }
        }