public IModule CompileModuleFromCode(string code, IMetadataReaderHost host) { // CoreAssembly = Host.LoadAssembly(Host.CoreAssemblySymbolicIdentity); var tree = SyntaxTree.ParseText(code); var comp = Compilation.Create("MyCompilation", new CompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddSyntaxTrees(tree) .AddReferences(new MetadataFileReference(typeof(object).Assembly.Location)); var outputFileName = Path.GetTempFileName();//Path.Combine(Path.GeGetTempPath(), "MyCompilation.lib"); var ilStream = new FileStream(outputFileName, FileMode.OpenOrCreate); var result = comp.Emit(ilStream); ilStream.Close(); if (!result.Success) { var aggregate = result.Diagnostics.Select(a => a.Info.GetMessage()).Aggregate((a, b) => a + "\n" + b); throw new InvalidProgramException(aggregate); } //using (var host = new PeReader.DefaultHost()) // { var module = host.LoadUnitFrom(outputFileName) as IModule; if (module == null || module == Dummy.Module || module == Dummy.Assembly) { throw new InvalidOperationException(outputFileName + " is not a PE file containing a CLR module or assembly."); } return Decompiler.GetCodeModelFromMetadataModel(host, module, null); // } }
public IModule CompileModuleFromCode(string code, IMetadataReaderHost host) { // CoreAssembly = Host.LoadAssembly(Host.CoreAssemblySymbolicIdentity); var tree = SyntaxTree.ParseText(code); var comp = Compilation.Create("MyCompilation", new CompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddSyntaxTrees(tree) .AddReferences(new MetadataFileReference(typeof(object).Assembly.Location)); var outputFileName = Path.GetTempFileName();//Path.Combine(Path.GeGetTempPath(), "MyCompilation.lib"); var ilStream = new FileStream(outputFileName, FileMode.OpenOrCreate); var result = comp.Emit(ilStream); ilStream.Close(); if (!result.Success) { var aggregate = result.Diagnostics.Select(a => a.Info.GetMessage()).Aggregate((a, b) => a + "\n" + b); throw new InvalidProgramException(aggregate); } //using (var host = new PeReader.DefaultHost()) // { var module = host.LoadUnitFrom(outputFileName) as IModule; if (module == null || module == Dummy.Module || module == Dummy.Assembly) { throw new InvalidOperationException(outputFileName + " is not a PE file containing a CLR module or assembly."); } return(Decompiler.GetCodeModelFromMetadataModel(host, module, null)); // } }
private static ISet <IAssembly> GetRootAssembliesFromOptions(ILGarbageCollectorOptions options, IMetadataReaderHost host) { ISet <IAssembly> rootAssemblies = new HashSet <IAssembly>(); foreach (var pathToAssembly in GetRootAssemblyPathsFromOptions(options)) { var assembly = host.LoadUnitFrom(pathToAssembly) as IAssembly; if (assembly == null) { throw new FileNotFoundException("Couldn't load assembly " + pathToAssembly); } rootAssemblies.Add(assembly); } return(rootAssemblies); }
private static ISet<IAssembly> GetRootAssembliesFromOptions(ILGarbageCollectorOptions options, IMetadataReaderHost host) { ISet<IAssembly> rootAssemblies = new HashSet<IAssembly>(); foreach (var pathToAssembly in GetRootAssemblyPathsFromOptions(options)) { var assembly = host.LoadUnitFrom(pathToAssembly) as IAssembly; if (assembly == null) { throw new FileNotFoundException("Couldn't load assembly " + pathToAssembly); } rootAssemblies.Add(assembly); } return rootAssemblies; }