Example #1
0
 public static ModuleDefinition ReadModule(Stream stream, ReaderParameters parameters)
 {
     Mixin.CheckStream(stream);
     if (!stream.CanRead || !stream.CanSeek)
     {
         throw new ArgumentException();
     }
     Mixin.CheckParameters(parameters);
     return(ModuleReader.CreateModuleFrom(ImageReader.ReadImageFrom(stream), parameters));
 }
Example #2
0
 public void Write(Stream stream, WriterParameters parameters)
 {
     Mixin.CheckStream(stream);
     if (!stream.CanWrite || !stream.CanSeek)
     {
         throw new ArgumentException();
     }
     Mixin.CheckParameters(parameters);
     ModuleWriter.WriteModuleTo(this, stream, parameters);
 }
        public static AssemblyDefinition CreateAssembly(AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
        {
            if (assemblyName == null)
            {
                throw new ArgumentNullException("assemblyName");
            }
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }
            Mixin.CheckParameters(parameters);
            if (parameters.Kind == ModuleKind.NetModule)
            {
                throw new ArgumentException("kind");
            }
            AssemblyDefinition assembly = ModuleDefinition.CreateModule(moduleName, parameters).Assembly;

            assembly.Name = assemblyName;
            return(assembly);
        }
Example #4
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);
            ModuleDefinition moduleDefinition = new ModuleDefinition
            {
                Name            = name,
                kind            = parameters.Kind,
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = ModuleCharacteristics.DynamicBase | ModuleCharacteristics.NoSEH | ModuleCharacteristics.NXCompat | ModuleCharacteristics.TerminalServerAware
            };

            if (parameters.AssemblyResolver != null)
            {
                moduleDefinition.assembly_resolver = parameters.AssemblyResolver;
            }
            if (parameters.MetadataResolver != null)
            {
                moduleDefinition.metadata_resolver = parameters.MetadataResolver;
            }
            if (parameters.MetadataImporterProvider != null)
            {
                moduleDefinition.metadata_importer = parameters.MetadataImporterProvider.GetMetadataImporter(moduleDefinition);
            }
            if (parameters.ReflectionImporterProvider != null)
            {
                moduleDefinition.reflection_importer = parameters.ReflectionImporterProvider.GetReflectionImporter(moduleDefinition);
            }
            if (parameters.Kind != ModuleKind.NetModule)
            {
                AssemblyDefinition assemblyDefinition = new AssemblyDefinition();
                moduleDefinition.assembly      = assemblyDefinition;
                moduleDefinition.assembly.Name = ModuleDefinition.CreateAssemblyName(name);
                assemblyDefinition.main_module = moduleDefinition;
            }
            moduleDefinition.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));
            return(moduleDefinition);
        }