Multiple modules in one assembly

34 views
Skip to first unread message

Yi Dong

unread,
Jul 25, 2019, 12:21:07 PM7/25/19
to mono-cecil
Hi,

If I want to create an assembly which contains two modules, say ModuleA and ModuleB. 

targetAssembly = AssemblyDefinition.CreateAssembly(assemblyNameDef, "ModuleA", ModuleKind.dll);
ModuleA = targetAssembly.MainModule;

ModuleB = ModuleDefinition.CreateModule("ModuleB", ModuleKind.NetModule);
targetAssembly.Modules.Add(ModuleB);

There are following methods define in ModuleA and ModuleB and the method in ModuleA will call into the one in ModuleB.

ModuleA
    Type1 {
      public void MethodX() 
      {
          Type2.MethodY
      }
    }


ModuleB
    Class Type2 {
       public static void MethodY() { }
    }


I will manually generate the IL code for both methods. 

     Type2 = ModuleB.GetType("namespace", "Type2");
     methodY = Type2.Methods.First(m => m.Name = "MethodY");

Following call hits NullReferenceException:

     MethodReference methodYRef = ModuleA.ImportReference(methodY);

The reason is ModuleB.Assembly is null. How can I correctly create an assembly with multiple modules and there are reference among these modules.

Thanks,
Yi
Reply all
Reply to author
Forward
0 new messages