Hi JB,

I have logging code in MethodWeaver.dll. This dll has class Lgoger and 
method Logtext. 

Using Mono Cecil I have injected code Logger.LogText() into another exe as 
external CustomerApp.exe. When running code getting error like "access to 
method Logger.LogText() failed."

It seems MethodWeaver.dll should be added as external assembly reference 
into CustomerApp.exe. Can you please help?

Thank you!

Regards,
Venu

On Tuesday, September 12, 2017 at 11:44:51 AM UTC+5:30, venu gopal Yela 
wrote:

> Thank you JB! 
>
> Got it. Method should be imported from Class.
>
> On Mon, Sep 11, 2017 at 9:57 PM, Jb Evain <[email protected]> wrote:
>
>> Hi,
>>
>> Unfortunately it's hard to say just from this code. The logical 
>> explanation would be that the module you're using to Import a reference is 
>> not the one you're using the reference into.
>>
>> Jb
>>
>> On Sun, Sep 10, 2017 at 7:29 PM, venu gopal Yela <[email protected]> 
>> wrote:
>>
>>> Hi JB,
>>>
>>> I have a class Logger with method LogText. This is to log text. I have 
>>> another class Math and has method add.
>>>
>>> I am trying to inject Logger.LogText method call in the add method. Here 
>>> is the code. 
>>>
>>> if (m.Name.Equals("add", StringComparison.CurrentCultureIgnoreCase))
>>>
>>> {
>>>
>>>        methodILProcessor = m.Body.GetILProcessor();
>>>
>>>        Instruction instrMsg = methodILProcessor.Create(OpCodes.Ldstr, 
>>> "method 
>>> - add()");
>>>
>>>        m.Body.Instructions.Insert(0, instrMsg);
>>>
>>>        Type classObj = getType("Logger");//get type using reflection
>>>
>>>        TypeReference typeRef = type.Module.Import(classObj);
>>>
>>>        MethodReference logMethodRef = 
>>> assembly.MainModule.Import(classObj.GetMethod("LogText", new Type[] { 
>>> typeof(string) }));
>>>
>>>        m.Body.Instructions.Insert(1, Instruction.Create(OpCodes.Call, 
>>> logMethodRef));
>>>
>>> }
>>>
>>> While saving assembly, getting error as below. Can you please help and 
>>> suggest where I am doing wrong?
>>>
>>> System.Void MyApp.Logger::LogText(System.String)' is declared in another 
>>> module and needs to be imported
>>>
>>> Thanks
>>> Venu
>>>
>>> On Monday, February 27, 2012 at 8:51:34 PM UTC+5:30, Remy Blok wrote:
>>>
>>>> I have a class with a default constructor. Based on an assembly 
>>>> attribute I'm trying to change the default constructor with additional 
>>>> arguments. The type of the arguments I'm adding is located in a 
>>>> different assembly. 
>>>> When I've change the assembly and try to save it I get an exception, 
>>>> which I cannot seem to fix: Member 'OtherAssembly.IViewModel' is 
>>>> declared in another module and needs to be imported. 
>>>>
>>>> The assembly that contains the Type is referenced by the assembly I'm 
>>>> changing. I also see this in the AssemblyReferences in the current 
>>>> module. I'm importing the Type into the current module when I get the 
>>>> TypeReference out of the Attribute. 
>>>>
>>>> It is probably someting simple but I can't seem to find it. What Am I 
>>>> doing wrong? 
>>>> Thanks for your help! 
>>>>
>>>> Below the code I'm using to test the issue. 
>>>> Assembly "Views.dll" I'm changing contains: 
>>>>
>>>> public class Class1 
>>>> { 
>>>>         public Class1() { } 
>>>> } 
>>>>
>>>> [assembly: OtherAssembly.ViewModel(typeof(Class1), 
>>>> typeof(OtherAssembly.IViewModel))] 
>>>>
>>>>
>>>> The "OtherAssembly" I'm referencing contains: 
>>>>
>>>> namespace OtherAssembly 
>>>> { 
>>>>         public interface IViewModel { } 
>>>>
>>>>         [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = 
>>>> true)] 
>>>>         public class ViewModelAttribute : Attribute 
>>>>         { 
>>>>                 public ViewModelAttribute(Type viewType, Type 
>>>> viewModelType) { } 
>>>>         } 
>>>> } 
>>>>
>>>> The Code I'm using in a separate tool to change the assembly: 
>>>>
>>>> var module = ModuleDefinition.ReadModule("Views.dll"); 
>>>>
>>>> //load Assembly attribute 
>>>> var attribute = module.Assembly.CustomAttributes.First(a => 
>>>> a.Constructor.DeclaringType.FullName == 
>>>> "OtherAssembly.ViewModelAttribute"); 
>>>> var customAttributeArguments = attribute.ConstructorArguments; 
>>>>
>>>> //get Arguments from the attribute 
>>>> var viewType = 
>>>> module.Import((TypeReference)customAttributeArguments[0].Value).Resolve(); 
>>>> // 
>>>> Resolves Class1 
>>>> var viewModelType = 
>>>> module.Import((TypeReference)customAttributeArguments[1].Value).Resolve(); 
>>>> // 
>>>> Resolves OtherAssembly.IViewModel 
>>>>
>>>> //find the constructor of "Class1" 
>>>> var ctor = viewType.Methods.First(m => m.IsConstructor && !m.IsStatic 
>>>> && m.Parameters.Count == 0); 
>>>>
>>>> //add "OtherAssembly.IViewModel" parameter to constructor 
>>>> var viewModelTypeParameter = new ParameterDefinition(viewModelType); 
>>>> ctor.Parameters.Add(viewModelTypeParameter); 
>>>>
>>>> //write causes exception: Member 'OtherAssembly.IViewModel' is 
>>>> declared in another module and needs to be imported 
>>>> module.Write("Views2.dll");
>>>
>>> -- 
>>> -- 
>>> --
>>> 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 [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> -- 
>> -- 
>> --
>> mono-cecil
>> --- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "mono-cecil" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/mono-cecil/E_dlFgLDPjc/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
-- 
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to