private static void LoadFile(string filepath) { try { AssemblyQuery aq = new AssemblyQuery(); var kk = aq.LoadFile(filepath, _resolver)?.DefinedTypes; if (kk == null) { return; } foreach (var tp in kk) { var singleton = tp.GetCustomAttribute <SingletonAttribute>() != null; var nc = tp.GetCustomAttributes <RegisterAttribute>(); if (nc?.Count() > 0) { foreach (RegisterAttribute n in nc) { if (n.InterfaceType.IsAssignableFrom(tp) || n.InterfaceType == tp || (n.InterfaceType.IsGenericTypeDefinition && tp.ImplementedInterfaces.Any(r => r.GetGenericTypeDefinition() == n.InterfaceType))) { _di.Register(n.InterfaceType, tp, singleton); } else { System.Diagnostics.Trace.TraceError($"{tp.FullName}未继承接口{n.InterfaceType.FullName},无法注册"); } } } } } catch { } }
private static void LoadDirectory(string directory) { try { AssemblyQuery aq = new AssemblyQuery(); var kk = aq.Search(directory, null, _resolver).SelectMany(s => s.DefinedTypes); foreach (var tp in kk) { var singleton = tp.GetCustomAttribute <SingletonAttribute>() != null; var nc = tp.GetCustomAttributes <RegisterAttribute>(); if (nc?.Count() > 0) { foreach (RegisterAttribute n in nc) { if (n.InterfaceType.IsAssignableFrom(tp) || n.InterfaceType == tp || (n.InterfaceType.IsGenericTypeDefinition && tp.ImplementedInterfaces.Any(r => r.GetGenericTypeDefinition() == n.InterfaceType))) { _di.Register(n.InterfaceType, tp, singleton); } else { System.Diagnostics.Trace.TraceError($"{tp.FullName}未继承接口{n.InterfaceType.FullName},无法注册"); } } } } } catch (Exception) { } }
IEnumerable <Type> LoadTypes() { var re = new Regex(classNameRegex, RegexOptions.Compiled); var aq = new AssemblyQuery(assembly); return(aq.GetAllTypes()); //aq.GetAllTypesMatching(t => t.FullName != null && re.IsMatch(t.FullName)); }
private static Assembly LoadFromPath(AssemblyQuery q) { if (!q.IsPath) throw new AssemblyNotFoundException( q.Query + " is not a valid assembly file (it should end with " + AssemblyQuery.ASSEMBLY_EXTENSION + ")" ); try { return Assembly.LoadFrom(q.Query); } catch (Exception e) { throw new AssemblyNotFoundException(q.Query, e); } }
public AssemblyQuery(AssemblyQuery other) { m_query = other.m_query; }
private static Assembly LoadAny(AssemblyQuery q) { try { if (q.IsPath) { // try to load it from a path return LoadFromPath(q); } else // partial name { // try loading it from the GAC or current app dir return LoadFromGacOrAppDir(q); } } catch (AssemblyNotFoundException) { try { // try to convert it to the other format (as a last resort) if (q.IsPath) { // load from path failed return LoadFromGacOrAppDir(q.ToPartialName()); } else { // load with partial name failed return LoadFromPath(q.ToPath()); } } catch (AssemblyNotFoundException) { throw new AssemblyNotFoundException( string.Format( "The assembly {0} could neither be found in the GAC or at an absolute path", q.Query ) ); } } }
private static Assembly LoadFromGacOrAppDir(AssemblyQuery q) { if (!q.IsPartialName) throw new AssemblyNotFoundException( q.Query + " is not a valid assembly name (it should NOT end with " + AssemblyQuery.ASSEMBLY_EXTENSION + ")" ); Assembly a = null; try { a = Assembly.Load(q.ToAssemblyName()); return a; } catch (Exception err) { try { // try to load it from the current application directory string assemblyPath = Path.Combine(Location.ApplicationDirectory, q.Query + AssemblyQuery.ASSEMBLY_EXTENSION); a = Assembly.LoadFrom(assemblyPath); return a; } catch(Exception e) { #if COMPACT throw new AssemblyNotFoundException(q.Query, e); #else // if all else failed, try LoadWithPartialName. This is // necessary for GTK# on Windows since it only provides // .NET 1.0 versions which are not found by Assembly.Load // on .NET 2.0. a = Assembly.LoadWithPartialName(q.Query); if (a != null) return a; else throw new AssemblyNotFoundException(q.Query, e); #endif } } }