Beispiel #1
0
        public static void Run(string assemblyPath, AnnotationsExporterParameters?parameters)
        {
            if (assemblyPath == null)
            {
                throw new ArgumentNullException(nameof(assemblyPath));
            }

            parameters ??= new AnnotationsExporterParameters();

            var assemblyResolver = new MyAssemblyResolver(assemblyPath, parameters.Libraries);

            foreach (var directory in parameters.AdditionalSearchDirectories)
            {
                assemblyResolver.AddSearchDirectory(directory);
            }

            var readerParameters = new ReaderParameters
            {
                ReadingMode      = ReadingMode.Deferred,
                ReadWrite        = parameters.WillSaveAssembly,
                AssemblyResolver = assemblyResolver,
                ReadSymbols      = true,
            };

            using var assembly = AssemblyDefinition.ReadAssembly(assemblyPath, readerParameters);
            if (parameters.ExportAnnotations)
            {
                ExportAnnotations(
                    assembly,
                    parameters.XmlPath ?? Path.ChangeExtension(assemblyPath, ".ExternalAnnotations.xml"));
            }

            if (parameters.StripAnnotations)
            {
                StripAnnotations(assembly);
            }

            if (!parameters.WillSaveAssembly)
            {
                return;
            }

            var writerParameters = new WriterParameters
            {
#if NETFRAMEWORK
                WriteSymbols = true,                                                // .NET Framework only runs on Windows
#else
                WriteSymbols = RuntimeInformation.IsOSPlatform(OSPlatform.Windows), // Symbol writer uses COM Interop, thus is Windows-only
#endif
            };

            assembly.Write(writerParameters);
        }
Beispiel #2
0
    static ModuleDefinition ReadModuleAndSymbols(Stream file)
    {
        var assembly = System.Reflection.Assembly.GetExecutingAssembly();

        if (assembly.GetType("Mono.Cecil.Pdb.NativePdbReaderProvider") == null ||
            assembly.GetType("Mono.Cecil.Mdb.MdbReaderProvider") == null)
        {
            // if cecil DLLs were merged into our assembly, then the above should work.
            // otherwise, we try to locate the needed types in external DLLs.

            if (Type.GetType("Mono.Cecil.Pdb.NativePdbReaderProvider, Mono.Cecil.Pdb") == null)
            {
                Console.WriteLine("Warning: cannot load Mono.Cecil.Pdb.dll");
            }

            if (Type.GetType("Mono.Cecil.Mdb.MdbReaderProvider, Mono.Cecil.Mdb") == null)
            {
                Console.WriteLine("Warning: cannot load Mono.Cecil.Mdb.dll");
            }
        }

        ModuleDefinition module = null;

        try
        {
            var myResolver = new MyAssemblyResolver(file);

            module = ModuleDefinition.ReadModule(file, myResolver.Parameters);

            myResolver.ConfigureDotNetCoreDirectories(module);
        }
        catch (BadImageFormatException e)
        {
            throw new JavaException($"input file '{(file as FileStream)?.Name}' "
                                    + "is not a valid .Net assembly: " + e.Message, null);
        }

        try
        {
            module.ReadSymbols();
        }
        catch (Mono.Cecil.Cil.SymbolsNotFoundException)
        {
        }

        return(module);
    }
		InMemoryModuleManager() {
			this.myAssemblyResolver = new MyAssemblyResolver();
		}
Beispiel #4
0
 InMemoryModuleManager()
 {
     this.myAssemblyResolver = new MyAssemblyResolver();
 }