Hey,

That's the intended behavior of System.Reflection. (This has nothing
to do with Cecil)

If you want to retrieve a non public method from System.Reflection,
you have to specify it so like this:

GetMethod("Check", BindingFlags.NonPublic | BindingFlags.Static)

(Replace Static by Instance if you want an Instance method).

That all being said, it's illegal in IL to call a private method from
another assembly/type, you're going to get a runtime exception.

Jb

On Fri, Nov 28, 2014 at 5:33 AM, SM <[email protected]> wrote:
> if  Method "Check" is private in acc_check.Math1
>  Instruction i2 = ilProcessor.Create(OpCodes.Call,
> md.Module.Import(typeof(acc_check.Math1).GetMethod("Check")));
> GetMethod   returns null
>
> If "Check" I spublic it works fine
>
> On Wednesday, November 26, 2014 2:54:36 PM UTC+5:30, Jb Evain wrote:
>>
>> Hi,
>>
>> Could you describe the issues you're having?
>>
>> Jb
>>
>> On Wed, Nov 26, 2014 at 6:29 AM, SM <[email protected]> wrote:
>> > Hi,
>> >
>> >    While injecting a private method call from different assembly ,
>> > getting
>> > issus
>> > Can we inject private method call using Cecil ?
>> >
>> >
>> >             foreach (ModuleDefinition mod in asm.Modules)
>> >             {
>> >                 foreach (TypeDefinition td in mod.Types)
>> >                 {
>> >                     if (td.Name == "<Module>")
>> >                     {
>> >                         continue;
>> >                     }
>> >
>> >                     foreach (MethodDefinition md in td.Methods)
>> >                     {
>> >                         if (md.Name.ToString() != ".ctor"  &&
>> > md.Name.ToString() != "Main")
>> >                         {
>> >                             ILProcessor ilProcessor =
>> > md.Body.GetILProcessor();
>> >                             Instruction i1 =
>> > ilProcessor.Create(OpCodes.Ldstr, md.ToString());
>> >                             injected = true;
>> >
>> >                             // Call the method which would call private
>> > method from different assembly
>> >                             string type_name = asm.Name.Name.ToString()
>> > +
>> > "." + td.Name.ToString();
>> >                             Instruction i2 =
>> > ilProcessor.Create(OpCodes.Call,
>> > md.Module.Import(typeof(acc_check.Math1).GetMethod("Check")));
>> >
>> > ilProcessor.InsertAfter(md.Body.Instructions[0],
>> > i2);
>> >
>> >                         }
>> >                     }
>> >                     td.Name = td.Name.ToString() + "_Accessor";
>> >                     td.Namespace = td.Namespace.ToString() +
>> > "_Accessor";
>> >                 }
>> >                 asm.Name.Name += "_Accessor";
>> >             }
>> >             asm.Write(accessor_dll ,new WriterParameters { WriteSymbols
>> > =
>> > true });
>> >
>> >
>> > "Check"  is a private method in  acc_check.Math1
>> >
>> > --
>> > --
>> > --
>> > 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 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 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