By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,929 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,929 IT Pros & Developers. It's quick & easy.

Reflection & method execution...

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"paccala" <tr****@hotmail.commoc> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2

P: n/a
Surely if mAInstance is private in Class B it can only be manipulated within
Class B.
Is that not the case?

"paccala" <tr****@hotmail.commoc> wrote in message
news:#5**************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #3

P: n/a
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);

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"paccala" wrote:
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

Nov 16 '05 #4

P: n/a
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"
<Ja******************************@diaIFYOUWISHTOSE NDMEAMAILREMOVETHISmetric.org>
wrote in message news:ce**********@titan.btinternet.com...
Surely if mAInstance is private in Class B it can only be manipulated
within
Class B.
Is that not the case?

"paccala" <tr****@hotmail.commoc> wrote in message
news:#5**************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #5

P: n/a
Nicole Calinoiu <ni*****@somewhere.net> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
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>
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicole Calinoiu <ni*****@somewhere.net> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

P: n/a
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.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"Jaxson Tammaru" wrote:
Surely if mAInstance is private in Class B it can only be manipulated within
Class B.
Is that not the case?

"paccala" <tr****@hotmail.commoc> wrote in message
news:#5**************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #8

P: n/a
Exactly how would you use ReflectionPermissionAttribute to prevent other
code
from reflecting into your low accessibility classes and members?
"C Addison Ritchie" <CA*************@discussions.microsoft.com> wrote in
message news:E0**********************************@microsof t.com...
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.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"Jaxson Tammaru" wrote:
Surely if mAInstance is private in Class B it can only be manipulated
within
Class B.
Is that not the case?

"paccala" <tr****@hotmail.commoc> wrote in message
news:#5**************@tk2msftngp13.phx.gbl...
> 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
>
>



Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.