I currently autogenerate some code and compile it into a dll. I then
want to move the types in this generated dll into another dll. I have
tried the following but the resulting assembly is invalid.

            AssemblyDefinition originalDll =
AssemblyDefinition.ReadAssembly(dirname + "Original.dll" );
            AssemblyDefinition generatedDll =
AssemblyDefinition.ReadAssembly(dirname + "Generated.dll" );

            List<TypeDefinition> typesToMoveToOriginalDll = new
List<TypeDefinition>();

            foreach (TypeDefinition type in new
List<TypeDefinition>( generatedDll.MainModule.Types) )
            {
                if (type.Name != "<Module>")
                {
                    typesToMoveToOriginalDll.Add(type);

                    generatedDll.MainModule.Types.Remove(type);
                }
            }

            foreach (TypeDefinition type in typesToMoveToOriginalDll)
            {
                originalDll.MainModule.Types.Add(type);
            }

            originalDll.Name.Name = "Original.Modified";
            originalDll.Write( dirname + "Original.Modified.dll" );

Peverify shows the following errors:

C:\Mono\cecil\Generated\bin\Debug>peverify Original.Modified.dll

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

[IL]: Error: [C:\Mono\cecil\Generated\bin\Debug
\Original.Modified.dll : Original.Entity::DoSomething][offset
0x00000003]
[found ref ('this' ptr) 'Original.Entity'][expected ref
'Generated.EntityData'] Unexpected type on the stack.
[IL]: Error: [C:\Mono\cecil\Generated\bin\Debug
\Original.Modified.dll : Original.Entity::DoSomething][offset
0x00000003]
 Field is not visible.
2 Error(s) Verifying Original.Modified.dll

Reflector shows that methods from types in the original dll have been
aliased onto the "imported" types from the generated dll.

Is there a simple way to move types between dlls, or do I need to
create a new TypeDefinition in the destination dll, and copy
FieldDefinitions, MethodDefinitions, etc ?

If I need to copy all definitions manually, do I need to iterate
through all instructions of each method and correct TypeReference
operands. For example, if code in the generated DLL referenced types
in the destination DLL, references will change from:

    class [Original]Original.MyFieldType
to:
    class Original.MyFieldType

since the referenced type would now be in the same dll as the
"imported" generated type.

-- 
--
mono-cecil

Reply via email to