コード例 #1
0
        public Type GetType(string name, bool throwOnError, bool ignoreCase)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }
            if (IgnoreType(name))
            {
                return(null);
            }
            if (ignoreCase)
            {
                Type cachedType;
                if (typeCacheIgnoreCase.TryGetValue(name, out cachedType))
                {
                    return(cachedType);
                }
            }
            else
            {
                Type cachedType;
                if (typeCache.TryGetValue(name, out cachedType))
                {
                    return(cachedType);
                }
            }
                        #if DEBUG
            if (!name.StartsWith("System.", StringComparison.Ordinal))
            {
                count++;
                LoggingService.Debug(count + " TypeResolutionService: Looking for " + name);
            }
                        #endif
            try {
                Type type = Type.GetType(name, false, ignoreCase);

                if (type == null)
                {
                    IProject p = CallingProject;
                    if (p != null)
                    {
                        ICompilation    compilation = SD.ParserService.GetCompilation(p);
                        ITypeDefinition definition  = ReflectionHelper.ParseReflectionName(name)
                                                      .Resolve(compilation).GetDefinition();
                        if (definition != null)
                        {
                            using (var resolver = new ProjectAssemblyResolver(compilation, this)) {
                                Assembly assembly = LoadAssembly(definition.ParentAssembly);
                                if (assembly != null)
                                {
                                    type = assembly.GetType(name, false, ignoreCase);
                                }
                            }
                        }
                    }
                }

                // type lookup for typename, assembly, xyz style lookups
                if (type == null)
                {
                    int idx = name.IndexOf(",", StringComparison.Ordinal);
                    if (idx > 0)
                    {
                        string[] splitName    = name.Split(',');
                        string   typeName     = splitName[0];
                        string   assemblyName = splitName[1].Substring(1);
                        Assembly assembly     = null;
                        try {
                            assembly = Assembly.Load(assemblyName);
                        } catch (Exception e) {
                            LoggingService.Error(e);
                        }
                        if (assembly != null)
                        {
                            string fileName = GetOriginalAssemblyFullPath(assembly);
                            if (!String.IsNullOrEmpty(fileName))
                            {
                                lock (loadedAssembliesForCurrentDocument) {
                                    loadedAssembliesForCurrentDocument [fileName] = GetHash(fileName);
                                }
                            }
                            lock (designerAssemblies) {
                                if (!designerAssemblies.Contains(assembly))
                                {
                                    designerAssemblies.Add(assembly);
                                }
                            }
                            type = assembly.GetType(typeName, false, ignoreCase);
                        }
                        else
                        {
                            type = Type.GetType(typeName, false, ignoreCase);
                        }
                    }
                }

                if (type == null)
                {
                    lock (designerAssemblies) {
                        foreach (Assembly asm in DesignerAssemblies)
                        {
                            try {
                                Type t = asm.GetType(name, false);
                                if (t != null)
                                {
                                    AddToCache(name, t, ignoreCase);
                                    return(t);
                                }
                            } catch (FileNotFoundException) {
                            } catch (FileLoadException) {
                            } catch (BadImageFormatException) {
                                // ignore assembly load errors
                            }
                        }
                    }
                }

                if (throwOnError && type == null)
                {
                    throw new TypeLoadException(name + " not found by TypeResolutionService");
                }
                AddToCache(name, type, ignoreCase);
                return(type);
            } catch (Exception e) {
                LoggingService.Error(e);
            }
            return(null);
        }
コード例 #2
0
        public Type GetType(string name, bool throwOnError, bool ignoreCase)
        {
            if (string.IsNullOrEmpty(name)) {
                return null;
            }
            if (IgnoreType(name)) {
                return null;
            }
            if (ignoreCase) {
                Type cachedType;
                if (typeCacheIgnoreCase.TryGetValue(name, out cachedType))
                    return cachedType;
            } else {
                Type cachedType;
                if (typeCache.TryGetValue(name, out cachedType))
                    return cachedType;
            }
            #if DEBUG
            if (!name.StartsWith("System.", StringComparison.Ordinal)) {
                count++;
                LoggingService.Debug(count + " TypeResolutionService: Looking for " + name);
            }
            #endif
            try {

                Type type = Type.GetType(name, false, ignoreCase);

                if (type == null) {
                    IProject p = CallingProject;
                    if (p != null) {
                        ICompilation compilation = SD.ParserService.GetCompilation(p);
                        ITypeDefinition definition = ReflectionHelper.ParseReflectionName(name)
                            .Resolve(compilation).GetDefinition();
                        if (definition != null) {
                            using (var resolver = new ProjectAssemblyResolver(compilation, this)) {
                                Assembly assembly = LoadAssembly(definition.ParentAssembly);
                                if (assembly != null) {
                                    type = assembly.GetType(name, false, ignoreCase);
                                }
                            }
                        }
                    }
                }

                // type lookup for typename, assembly, xyz style lookups
                if (type == null) {
                    int idx = name.IndexOf(",", StringComparison.Ordinal);
                    if (idx > 0) {
                        string[] splitName = name.Split(',');
                        string typeName     = splitName[0];
                        string assemblyName = splitName[1].Substring(1);
                        Assembly assembly = null;
                        try {
                            assembly = Assembly.Load(assemblyName);
                        } catch (Exception e) {
                            LoggingService.Error(e);
                        }
                        if (assembly != null) {
                            string fileName = GetOriginalAssemblyFullPath(assembly);
                            if (!String.IsNullOrEmpty(fileName)) {
                                lock (loadedAssembliesForCurrentDocument) {
                                    loadedAssembliesForCurrentDocument [fileName] = GetHash(fileName);
                                }
                            }
                            lock (designerAssemblies) {
                                if (!designerAssemblies.Contains(assembly))
                                    designerAssemblies.Add(assembly);
                            }
                            type = assembly.GetType(typeName, false, ignoreCase);
                        } else {
                            type = Type.GetType(typeName, false, ignoreCase);
                        }
                    }
                }

                if (type == null) {
                    lock (designerAssemblies) {
                        foreach (Assembly asm in DesignerAssemblies) {
                            try {
                                Type t = asm.GetType(name, false);
                                if (t != null) {
                                    AddToCache(name, t, ignoreCase);
                                    return t;
                                }
                            } catch (FileNotFoundException) {
                            } catch (FileLoadException) {
                            } catch (BadImageFormatException) {
                                // ignore assembly load errors
                            }
                        }
                    }
                }

                if (throwOnError && type == null)
                    throw new TypeLoadException(name + " not found by TypeResolutionService");
                AddToCache(name, type, ignoreCase);
                return type;
            } catch (Exception e) {
                LoggingService.Error(e);
            }
            return null;
        }