void BindToObservableCollection(AssemblyList collection) { this.Children.Clear(); this.Children.AddRange(collection.GetAssemblies().Select(a => new AssemblyTreeNode(a))); collection.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: this.Children.InsertRange(e.NewStartingIndex, e.NewItems.Cast <LoadedAssembly>().Select(a => new AssemblyTreeNode(a))); break; case NotifyCollectionChangedAction.Remove: this.Children.RemoveRange(e.OldStartingIndex, e.OldItems.Count); break; case NotifyCollectionChangedAction.Replace: this.Children.RemoveRange(e.OldStartingIndex, e.OldItems.Count); this.Children.InsertRange(e.NewStartingIndex, e.NewItems.Cast <LoadedAssembly>().Select(a => new AssemblyTreeNode(a))); break; case NotifyCollectionChangedAction.Move: throw new NotImplementedException(); case NotifyCollectionChangedAction.Reset: this.Children.Clear(); this.Children.AddRange(collection.GetAssemblies().Select(a => new AssemblyTreeNode(a))); break; default: throw new NotSupportedException("Invalid value for NotifyCollectionChangedAction"); } }; }
private IEnumerable <ILSpyTreeNode> FetchChildren(CancellationToken cancellationToken) { // FetchChildren() runs on the main thread; but the enumerator will be consumed on a background thread var assemblies = list.GetAssemblies().Select(node => node.GetModuleDefinitionOrNull()).Where(asm => asm != null).ToArray(); return(FindDerivedTypes(type, assemblies, cancellationToken)); }
IEnumerable <PEFile> GetReferencingModules(PEFile self, CancellationToken ct) { yield return(self); foreach (var assembly in AssemblyList.GetAssemblies()) { ct.ThrowIfCancellationRequested(); bool found = false; var module = assembly.GetPEFileOrNull(); if (module == null || !module.IsAssembly) { continue; } var resolver = assembly.GetAssemblyResolver(); foreach (var reference in module.AssemblyReferences) { using (LoadedAssembly.DisableAssemblyLoad()) { if (resolver.Resolve(reference) == self) { found = true; break; } } } if (found && ModuleReferencesScopeType(module.Metadata, typeScope.Name, typeScope.Namespace)) { yield return(module); } } }
IEnumerable <PEFile> GetReferencingModules(PEFile self, CancellationToken ct) { yield return(self); string reflectionTypeScopeName = typeScope.Name; if (typeScope.TypeParameterCount > 0) { reflectionTypeScopeName += "`" + typeScope.TypeParameterCount; } var toWalkFiles = new Stack <PEFile>(); var checkedFiles = new HashSet <PEFile>(); toWalkFiles.Push(self); checkedFiles.Add(self); do { PEFile curFile = toWalkFiles.Pop(); foreach (var assembly in AssemblyList.GetAssemblies()) { ct.ThrowIfCancellationRequested(); bool found = false; var module = assembly.GetPEFileOrNull(); if (module == null || !module.IsAssembly) { continue; } if (checkedFiles.Contains(module)) { continue; } var resolver = assembly.GetAssemblyResolver(); foreach (var reference in module.AssemblyReferences) { using (LoadedAssembly.DisableAssemblyLoad(AssemblyList)) { if (resolver.Resolve(reference) == curFile) { found = true; break; } } } if (found && checkedFiles.Add(module)) { if (ModuleReferencesScopeType(module.Metadata, reflectionTypeScopeName, typeScope.Namespace)) { yield return(module); } if (ModuleForwardsScopeType(module.Metadata, reflectionTypeScopeName, typeScope.Namespace)) { toWalkFiles.Push(module); } } } } while (toWalkFiles.Count > 0); }
public void returns_two_if_gived_was_two_types(Type t1, Type t2) { AssemblyList list = exec(new List <Type>() { t1, t2 }.ToArray()); Assert.IsTrue(list.GetAssemblies().Count() == 2); }
public IEnumerable <PEFile> GetAllModules() { foreach (var module in AssemblyList.GetAssemblies()) { var file = module.GetPEFileOrNull(); if (file == null) { continue; } yield return(file); } }
IEnumerable <PEFile> GetModuleAndAnyFriends(ITypeDefinition typeScope, CancellationToken ct) { var self = typeScope.ParentModule.PEFile; yield return(self); var typeProvider = MetadataExtensions.MinimalAttributeTypeProvider; var attributes = self.Metadata.CustomAttributes.Select(h => self.Metadata.GetCustomAttribute(h)) .Where(ca => ca.GetAttributeType(self.Metadata).GetFullTypeName(self.Metadata).ToString() == "System.Runtime.CompilerServices.InternalsVisibleToAttribute"); var friendAssemblies = new HashSet <string>(); foreach (var attribute in attributes) { string assemblyName = attribute.DecodeValue(typeProvider).FixedArguments[0].Value as string; assemblyName = assemblyName.Split(',')[0]; // strip off any public key info friendAssemblies.Add(assemblyName); } if (friendAssemblies.Count > 0) { IEnumerable <LoadedAssembly> assemblies = AssemblyList.GetAssemblies(); foreach (var assembly in assemblies) { ct.ThrowIfCancellationRequested(); if (friendAssemblies.Contains(assembly.ShortName)) { var module = assembly.GetPEFileOrNull(); if (module == null) { continue; } if (ModuleReferencesScopeType(module.Metadata, typeScope.Name, typeScope.Namespace)) { yield return(module); } } } } }
private static void Main(string[] args) { // Console.WriteLine("Args: {0}", string.Join(", ", args)); // Console.Read(); // AttachConsoleWriter(); string appPath = null; string slnName = null; string libPath = null; string expOpt = null; string outLanguageType = LAN_TYPE_CSHARP; DecompilerSettings ds = new DecompilerSettings(); ds.AnonymousMethods = true; ds.AsyncAwait = true; ds.YieldReturn = true; string onlyDecomileClassName = null; bool dllPathOnTop = true; List <string> onlyDecompilingFileNameList = new List <string>(); //parsing args foreach (string x in args) { if (x.StartsWith("-")) { switch (x) { case "-n": case "-l": case "-t": case "-C": case "-D": expOpt = x; continue; case "-q": dllPathOnTop = false; continue; default: if (x.Length < 2) { Console.WriteLine(" Unexpected options " + x); showUsage(); return; } for (int i = 1; i < x.Length; i++) { if (!praseDecompileSetting(x[i], ds)) { Console.WriteLine(" Unexpected options " + x); showUsage(); return; } } continue; } } else if (expOpt != null) { switch (expOpt) { case "-n": slnName = x; expOpt = null; break; case "-l": libPath = x; expOpt = null; break; case "-t": if (x != LAN_TYPE_CSHARP && x != LAN_TYPE_IL) { Console.WriteLine(" Unexpected Output language type: " + x); showUsage(); return; } outLanguageType = x; expOpt = null; break; case "-C": onlyDecomileClassName = x; expOpt = null; break; case "-D": onlyDecompilingFileNameList.Add(x); break; default: showUsage(); expOpt = null; return; } } else { if (appPath == null) { appPath = x; continue; } else { Console.WriteLine(" Unexpected options " + x); showUsage(); return; } } } if (appPath == null) { Console.WriteLine("directory/to/all/your/dll missing"); showUsage(); return; } if (slnName == null && outLanguageType == LAN_TYPE_CSHARP) { Console.WriteLine("Solution Name missing"); showUsage(); return; } Console.WriteLine("Decompiling all dll in " + appPath); Console.WriteLine("Please wait..."); // Extensions.WriteRead($"Path: {appPath}"); DirectoryInfo di = new DirectoryInfo(appPath); appPath = di.FullName; FileInfo[] dllFileInfoList = di.GetFiles("*.dll"); FileInfo[] exeFileInfoList = di.GetFiles("*.exe"); AssemblyList asmlist = new AssemblyList("mylistname"); foreach (var dllfile in dllFileInfoList) { bool bDecompile = isDecompilingFile(dllfile.FullName, onlyDecompilingFileNameList); asmlist.OpenAssembly(dllfile.FullName, !bDecompile); } foreach (var dllfile in exeFileInfoList) { bool bDecompile = isDecompilingFile(dllfile.FullName, onlyDecompilingFileNameList); asmlist.OpenAssembly(dllfile.FullName, !bDecompile); } if (libPath != null) { di = new DirectoryInfo(libPath); libPath = di.FullName; dllFileInfoList = di.GetFiles("*.dll"); foreach (var dllfile in dllFileInfoList) { asmlist.OpenAssembly(dllfile.FullName, true); } } StringBuilder projSln = new StringBuilder(); projSln.Append("Microsoft Visual Studio Solution File, Format Version 11.00\n# Visual Studio 2010\n"); StringBuilder globSec = new StringBuilder(); Guid slnProjGuid = Guid.NewGuid(); int num = 0; LoadedAssembly[] ls = asmlist.GetAssemblies(); var decompilationOptions = new DecompilationOptions(); decompilationOptions.AssemblyFileNameOnTop = dllPathOnTop; decompilationOptions.FullDecompilation = true; decompilationOptions.assenmlyList = asmlist; decompilationOptions.DecompilerSettings = ds; decompilationOptions.IncludedClassName = onlyDecomileClassName; if (outLanguageType == LAN_TYPE_CSHARP) { foreach (LoadedAssembly asm in ls) { if (asm.IsAutoLoaded) { continue; } string projectPath = appPath + "/" + asm.ShortName; if (!Directory.Exists(projectPath)) { Directory.CreateDirectory(projectPath); } string projectFileName = projectPath + "/" + asm.ShortName + ".csproj"; asm.ProjectGuid = Guid.NewGuid(); asm.ProjectFileName = projectFileName; } } foreach (LoadedAssembly asm in ls) { num++; Console.WriteLine(asm.FileName + " " + num + "/" + ls.Length); if (asm.IsAutoLoaded) { continue; } if (outLanguageType == LAN_TYPE_CSHARP) { var csharpLanguage = new CSharpLanguage(); var textOutput = new PlainTextOutput(); decompilationOptions.SaveAsProjectDirectory = appPath + "/" + asm.ShortName; csharpLanguage.DecompileAssembly(asm, textOutput, decompilationOptions); File.WriteAllText(asm.ProjectFileName, textOutput.ToString()); Guid createdProjGuid = asm.ProjectGuid; projSln.Append(" Project(\"{"); projSln.Append(slnProjGuid.ToString()); projSln.Append("}\") = \""); projSln.Append(asm.ShortName); projSln.Append("\", \""); projSln.Append(asm.ShortName + "/" + asm.ShortName + ".csproj"); projSln.Append("\", \"{"); projSln.Append(createdProjGuid.ToString()); projSln.Append("}\"\n"); projSln.Append("EndProject\n"); globSec.Append(" {" + createdProjGuid.ToString() + "}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n"); globSec.Append(" {" + createdProjGuid.ToString() + "}.Debug|Any CPU.Build.0 = Debug|Any CPU\n"); globSec.Append(" {" + createdProjGuid.ToString() + "}.Release|Any CPU.ActiveCfg = Release|Any CPU\n"); globSec.Append(" {" + createdProjGuid.ToString() + "}.Release|Any CPU.Build.0 = Release|Any CPU\n"); } else { var ilLanguage = new ILLanguage(true); var textOutput = new PlainTextOutput(); ilLanguage.DecompileAssembly(asm, textOutput, decompilationOptions); string ilFileName = appPath + "/" + asm.ShortName + ".il"; File.WriteAllText(ilFileName, textOutput.ToString()); } } if (outLanguageType == LAN_TYPE_CSHARP) { projSln.Append("Global\n"); projSln.Append("GlobalSection(SolutionConfigurationPlatforms) = preSolution\n"); projSln.Append("\t\t\t\tDebug|Any CPU = Debug|Any CPU\n"); projSln.Append("\t\t\t\tRelease|Any CPU = Release|Any CPU\n"); projSln.Append("EndGlobalSection\n"); projSln.Append("GlobalSection(ProjectConfigurationPlatforms) = postSolution\n"); projSln.Append(globSec.ToString()); projSln.Append("EndGlobalSection\n"); projSln.Append("GlobalSection(MonoDevelopProperties) = preSolution\n"); projSln.Append("\nEndGlobalSection\n"); projSln.Append("EndGlobal\n\t\t"); string slnFileName = appPath + "/" + slnName + ".sln"; File.WriteAllText(slnFileName, projSln.ToString()); } }