Ejemplo n.º 1
0
        /// <summary>
        /// Create a module initializer in an assembly that contains a global::Module.Init() method.
        /// </summary>
        /// <param name="targetAssembly">The assembly to create the module initializer.</param>
        static void Main(FileInfo targetAssembly)
        {
            //https://www.coengoedegebure.com/module-initializers-in-dotnet/
            Console.WriteLine($"Processing {targetAssembly}");
            using (var symbolStream = GetSymbolInformation(targetAssembly.FullName, out ISymbolReaderProvider reader,
                                                           out ISymbolWriterProvider writer))
            {
                var module = ModuleDefinition.ReadModule(targetAssembly.FullName, new ReaderParameters
                {
                    ReadSymbols          = symbolStream != null || reader is EmbeddedPortablePdbReaderProvider,
                    SymbolReaderProvider = reader,
                    SymbolStream         = symbolStream,
                    InMemory             = true,
                });

                var type = module.GetType("Module");
                if (type == null)
                {
                    Console.WriteLine("Could not find global::Module class");
                    return;
                }

                var initMethod = type.GetMethods().FirstOrDefault(x => x.Name == "Init");
                if (initMethod == null)
                {
                    Console.WriteLine("Could not find Init() method on global::Module class");
                    return;
                }


                var assemblyModuleClass = module.GetType("<Module>");
                if (assemblyModuleClass == null)
                {
                    Console.WriteLine("Could not find <Module> class");
                    return;
                }

                const MethodAttributes Attributes =
                    MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
                var cctor = new MethodDefinition(".cctor", Attributes, module.ImportReference(typeof(void)));
                var il    = cctor.Body.GetILProcessor();
                il.Append(il.Create(OpCodes.Call, initMethod));
                il.Append(il.Create(OpCodes.Ret));

                assemblyModuleClass.Methods.Add(cctor);

                module.Write(targetAssembly.FullName, new WriterParameters
                {
                    WriteSymbols         = writer != null,
                    SymbolWriterProvider = writer,
                });
                Console.WriteLine($"Wrote updated assembly '{targetAssembly.FullName}'");
            }
        }
Ejemplo n.º 2
0
 public static AssemblyDefinition ReadAssembly(Stream stream, ReaderParameters parameters)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(stream, parameters)));
 }
Ejemplo n.º 3
0
 public static AssemblyDefinition ReadAssembly(Stream stream)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(stream)));
 }
Ejemplo n.º 4
0
 public static AssemblyDefinition ReadAssembly(string fileName, ReaderParameters parameters)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(fileName, parameters)));
 }
Ejemplo n.º 5
0
 public static AssemblyDefinition ReadAssembly(string fileName)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(fileName)));
 }
Ejemplo n.º 6
0
        AssemblyDefinition GetAssembly(string file, ReaderParameters parameters)
        {
            if (parameters.AssemblyResolver == null)
            {
                parameters.AssemblyResolver = this;
            }

            var assemblyDef = ModuleDefinition.ReadModule(file, parameters).Assembly;

            if (assemblyDef == null || parameters.TargetArchitecture == TargetArchitecture.Any)
            {
                return(assemblyDef);
            }

            switch (parameters.TargetArchitecture)
            {
            case TargetArchitecture.ILOnly:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                if (!(ilOnly && !is32BitRequired && !is64Bit))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x86:
            {
                var is64Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32Bit = !is64Bit;

                if (!is32Bit)
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x86Only:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32Bit         = !is64Bit;
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;

                if (!(is32Bit && is32BitRequired))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x64:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                if (!(is64Bit || (ilOnly && !is32BitRequired)))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x64Only:
            {
                var is64Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);

                if (!is64Bit)
                {
                    assemblyDef = null;
                }

                break;
            }
            }

            return(assemblyDef);
        }
Ejemplo n.º 7
0
        public static TypeDefinition ToDefinition(this Type self)
        {
            var module = ModuleDefinition.ReadModule(self.Module.FullyQualifiedName);

            return((TypeDefinition)module.LookupToken(self.MetadataToken));
        }