public static Assembly[] LoadReferencedAssemblies(Assembly a, bool includethis, bool SkipMissingRefences = false) { var r = LoadDependencies(a, a, includethis, null, SkipMissingRefences).Distinct( new LoadDependenciesValue.EqualityComparer() ).ToArray().ToDictionary(i => i.Assembly, i => i.Dependencies.Distinct().ToArray()); var k = r.Keys.OrderByDescending( kk => { // fixme: we should apply namespace filters here var any = kk.GetTypes().Any( kkk => { var sa = ScriptAttribute.OfProvider(kkk); if (sa == null) { return(false); } // if the implements type is in the same assembly // we skip this rule! if (sa.Implements == null) { return(false); } return(kkk.Assembly != sa.Implements.Assembly); } ); return(any); } ).ToArray(); // this is almost in the correct order // we need to consider that some assemlies are out of band // which mean they are only after the BCLImplementation // assemblies return(k); }
public static Type[] FindTypes(Assembly a, ScriptType _scripttype) { List <Type> MyTypes = new List <Type>(); if (ScriptAttribute.OfProvider(a) == null) { System.Diagnostics.Debugger.Break(); } else { ScriptTypeFilterAttribute[] f = ScriptTypeFilterAttribute.ArrayOf(a, _scripttype); if (f.Length > 0) { #region filters are defined //Console.WriteLine("scripttype filters defined for assambly {0}", a); //foreach (ScriptTypeFilterAttribute fi in f) // Console.WriteLine(fi); foreach (Type z in a.GetTypes()) { // lets detect anonymous types if (IsAnonymousType(z)) { // found one, yuppei! MyTypes.Add(z); continue; } // check for type filter, and if off bounds then skip bool bOutOfNamespace = true; foreach (ScriptTypeFilterAttribute fi in f) { if (fi.MatchType(z)) { //Console.WriteLine("+ {0} : {1} [{2}, {3}]", z.FullName, fi.Filter, fi.Type, _scripttype); bOutOfNamespace = false; break; } } if (bOutOfNamespace) { //Console.WriteLine("- {0}", z.FullName); continue; } ScriptAttribute o = ScriptAttribute.OfProvider(z); //ScriptAttribute o = ScriptAttribute.Of(z, true); if (o != null) { MyTypes.Add(z); continue; } if (z.IsEnum || IsCompilerGenerated(z)) { if (IsNestedTypeOfScriptType(z)) { MyTypes.Add(z); } else { Console.WriteLine(" type ignored : {0}", z.FullName); } } } #endregion } else { var Obfuscation = (ObfuscationAttribute)a.GetCustomAttributes(typeof(ObfuscationAttribute), false).FirstOrDefault(); if (Obfuscation != null) { if (Obfuscation.Feature == "script") { return(a.GetTypes()); } } } } return(MyTypes.ToArray()); }
static IEnumerable <LoadDependenciesValue> LoadDependencies( Assembly context, Assembly a, bool includethis, Action <Assembly> h, bool SkipMissingRefences = false ) { //Console.WriteLine("enter LoadDependencies " // + new // { // Environment.CurrentDirectory, // context = context.Location, // a = a.Location // }); var r = new LoadDependenciesValue(); r.Assembly = a; Action <Assembly> Add = n => { r.Dependencies = r.Dependencies.Concat(new[] { n }).ToArray(); if (h != null) { h(n); } }; var ReferencedAssemblies = a.GetReferencedAssemblies() .Where(k => k.Name != a.GetName().Name) .Select( k => { //Console.WriteLine("LoadDependencies " + new { k.Name }); try { //Additional information: Could not load file or assembly 'System.Data.SQLite, Version=1.0.90.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) //var LocalCopyHint = new FileInfo(a.Location).Directory + "/" + k.Name + ".dll"; //if (File.Exists(LocalCopyHint)) //{ // // 20131226 // return Assembly.LoadFrom(LocalCopyHint); //} return(AppDomain.CurrentDomain.Load(k)); } catch { if (SkipMissingRefences) { return(null); } throw; } } ).Where(k => k != null); var _as = ScriptAttribute.OfProvider(a); if (_as != null) { if (_as.ScriptLibraries != null) { ReferencedAssemblies = ReferencedAssemblies.Concat(_as.ScriptLibraries.Select(k => k.Assembly)); } } foreach (var x in ReferencedAssemblies) { if (ScriptAttribute.Of(x) == null) { // either it is not a script library // or it is in regards to context var cs = ScriptAttribute.OfProvider(context); if (cs != null) { if (cs.ScriptLibraries != null) { if (cs.ScriptLibraries.Any(k => k.Assembly == x)) { goto ContinueAdd; } } } continue; } ContinueAdd: Add(x); foreach (var v in LoadDependencies(context, x, true, Add)) { yield return(v); } } if (includethis) { yield return(r); } }