Reflection & method execution...

P

paccala

Ciao
Being google-ing since hours and still I'm not able to
solve my problem...
I have following hierarchy:

class A
{
public void myMethod();
}

class B
{
private A mAInstance = new A();
}

from within class C, I would like to invoke the method
myMethod() in the mAInstance instance contained in class B.
I am able to get the FieldInfo of mAInstance, but it does not
work as object instance when passed to MethodInfo.Invoke...

Thanks in advance, any hint would really be appreciated

mauro
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi paccala,

Since you have FieldInfo for mAInstance call its GetValue method to get
class A object reference. This reference you should pass as a parameter to
myMethod's MethodInfo
 
J

Jaxson Tammaru

Surely if mAInstance is private in Class B it can only be manipulated within
Class B.
Is that not the case?
 
G

Guest

I put this together pretty quick... the last three lines will get you there.

ClassB b = new ClassB();
Type t = b.GetType();

FieldInfo fi = t.GetField("mAInstance", BindingFlags.NonPublic | BindingFlags.Instance);

Type a = fi.FieldType;

MethodInfo mi = a.GetMethod("MyMethod");
mi.Invoke(fi.GetValue(b), null);
 
N

Nicole Calinoiu

Nope. Reflection allows for use of classes and members beyond their
declared accessility. The relevant reflection methods have overloads with
BindingFlags arguments that allow you to specify the desired target's
accessibility level(s).


"Jaxson Tammaru"
 
J

Jon Skeet [C# MVP]

Nicole Calinoiu said:
Nope. Reflection allows for use of classes and members beyond their
declared accessility. The relevant reflection methods have overloads with
BindingFlags arguments that allow you to specify the desired target's
accessibility level(s).

You need to have the relevant security permissions, of course...
 
G

Guest

With Reflection you can access anything regardless of accessibility (caveat coming). In this case you are able to get the instance of mAInstance and use it like it was a public member of ClassB.

(caveat) But you can use the ReflectionPermissionAttribute to control reflection so that if a requestor doesn't have the correct permissions they are not allowed to "reflect" on your class and bypass your designed accessibility of class/instance members.
 
N

Nicole Calinoiu

Of course, but that's not exactly a huge limitation since all locally
installed software will have adequate permissions under the default policy
scheme. At any rate, as well-behaved developers, we should be far more
concerned about others bypassing our intended interfaces than with the
permissions that our code might need in order to do the same... <gdr>
 
N

Nicole Calinoiu

Exactly how would you use ReflectionPermissionAttribute to prevent other
code
from reflecting into your low accessibility classes and members?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top