1

Assembly.write method throws the following error

Member 'XXX.LoggingAspectAttribute' is declared in another module and needs to be imported

Ive included the following lines in my code

 var type = _module.Import(typeof(LoggingAspectAttribute));
 _module.Import(type.Resolve().Methods.First(m => m.Name == "OnExecute"));
 _module.Import(type.Resolve().Methods.First(m => m.Name == "OnException"));
 _module.Import(type.Resolve().Methods.First(m => m.Name == "OnComplete"));
 _module.Import(typeof(LoggingAspectAttribute)).Resolve();

where module is defined as the main module of the assembly

_module = ModuleDefinition.ReadModule(filename, new ReaderParameters()
        {
            ReadSymbols = true
        });

Any ideas on how to fix this error?

Mr Anderson
  • 2,200
  • 13
  • 23
Vasudha Gupta
  • 171
  • 11
  • 1
    Here's a user with a similar issue using Mono.cecil: http://stackoverflow.com/questions/34879710/member-is-declared-in-another-module-and-needs-to-be-imported. – Ash May 27 '16 at 06:37
  • Its not clear from that question whats the fix to my issue. – Vasudha Gupta May 27 '16 at 09:24
  • @VasudhaGupta try to remove tha last row `_module.Import(typeof(LoggingAspectAttribute)).Resolve();` – Dudi Keleti May 27 '16 at 09:56
  • @dudiKeleti Even after removing the same error is there. – Vasudha Gupta May 27 '16 at 10:00
  • @VasudhaGupta Do you have any usage of the imported method? Can you show us the fuul code? – Dudi Keleti May 27 '16 at 10:03
  • Here is the code that i am using .. https://github.com/scmccart/Reweave.. When i try to put LoggerAspectAttribute in another assembly - Reweave.Core it gives error in the Assembly.write method. – Vasudha Gupta May 29 '16 at 14:04
  • Maybe have a look at this [git repo](https://github.com/markusschweitzer/ILRewriter) it is pretty similar. – Markus Jun 03 '16 at 04:31

1 Answers1

0

ModuleDefinition.Import() does not import anything into the module, it simply returns a reference to the metadata from the module's point of view.

Try this:

TypeReference type = _module.Import(typeof(LoggingAspectAttribute));
MethodReference onExecute = _module.Import(typeof(LoggingAspectAttribute).GetMethod("OnExecute"));
MethodReference onException = _module.Import(typeof(LoggingAspectAttribute).GetMethod("OnException"));
MethodReference onComplete = _module.Import(typeof(LoggingAspectAttribute).GetMethod("OnComplete"));
Mr Anderson
  • 2,200
  • 13
  • 23