Hi,

I have a simple task to create a type and save it to dll.

        public static void WriteAType()
        {
            AssemblyNameDefinition name = new 
AssemblyNameDefinition("CecilAssembly", new Version(1, 0));
            AssemblyDefinition assembly = 
AssemblyDefinition.CreateAssembly(name, "CecilAssembly", ModuleKind.Dll);
            ModuleDefinition module = 
ModuleDefinition.CreateModule("myModule", ModuleKind.Dll);
            assembly.Modules.Add(module);
            TypeReference objectReference = module.Import(typeof (object));
            var met = objectReference.Resolve().Methods;
            TypeDefinition typeDef = new TypeDefinition("sample", "Main", 
TypeAttributes.Class | TypeAttributes.Public);
            AddEmptyConstructor(typeDef, met[0], module);
            TypeDefinition resolved = typeDef.Resolve();
            module.Types.Add(resolved);
            assembly.Write("CecilAssembly.dll");
        }

        private static void AddEmptyConstructor(TypeDefinition type, 
MethodReference baseEmptyConstructor, ModuleDefinition module)
        {
            const MethodAttributes methodAttributes =
                MethodAttributes.Public | MethodAttributes.HideBySig | 
MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
            var method = new MethodDefinition(".ctor", methodAttributes, 
module.TypeSystem.Void);
            
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, 
baseEmptyConstructor));
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            type.Methods.Add(method);
        }

when I open ilSpy on CecilAssembly.dll I see empty one.

I found one snippet where, one guy, creates a type and saves it to 
MainModule at AssemblyDefinition and actualy ilSpy shows content of that 
type.
I modified code above a bit, here is a snippet:

        public static void ExploreCecilType()
        {
            AssemblyNameDefinition name = new 
AssemblyNameDefinition("mycecil", new Version(1, 0));
            AssemblyDefinition assembly = 
AssemblyDefinition.CreateAssembly(name, "myModule", ModuleKind.Console);
            ModuleDefinition module = assembly.MainModule;
            TypeReference objectRef = module.Import(typeof (object));

            TypeDefinition fooType = new TypeDefinition("", "Foo", 
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AnsiClass | 
TypeAttributes.AutoClass, objectRef);
            module.Types.Add(fooType);
            AddEmptyConstructor(fooType, objectRef.Resolve().Methods[0], 
module);
            assembly.Write("mycecil.dll");
}

When I launch this, it gives me:
Unhandled Exception: System.ArgumentException: Member 'System.Void 
System.Object
::.ctor()' is declared in another module and needs to be imported

didn't I just import it with "TypeReference objectRef = 
module.Import(typeof (object));"?

Is there a way to create a type and save it? Why modules are not saved when 
I create my own? How to reference a mscorlib? 

Thanks.

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mono-cecil+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to