IsFamily with Friend and MethodInfo

G

Guest

Question
Hello,

I'm trying to get a MethodInfo class with this code:

---------------------------------------------
Imports System.Reflection

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim myType As Type = GetType(A)
Dim mi As MethodInfo = myType.GetMethod("firstMethod",
BindingFlags.NonPublic Or BindingFlags.Public)

If Not mi Is Nothing Then MsgBox("we have it !")

End Sub

End Class

Public Class A
Friend Sub firstMethod()
MsgBox("message from class A")
End Sub
End Class
---------------------------------------------
But methodinfo is always Nothing !

If I make firstMethod a PUBLIC sub it works without the binding flags, but
what I want is to obtain a methodInfo instantiated for a Friend method, and
its property "isFamily" being TRUE...


Can you help me, please ?

Thanks in advance,

Roger Tranchez
..NET 2005 and DB developer
 
M

Mattias Sjögren

Roger,
But methodinfo is always Nothing !

If I make firstMethod a PUBLIC sub it works without the binding flags, but
what I want is to obtain a methodInfo instantiated for a Friend method, and
its property "isFamily" being TRUE...


Can you help me, please ?


Try including BindingFlags.Instance.


Mattias
 
G

Guest

Hi,

It works, thanks a lot. I searched the web for an explanation of
"BindingFlags.Instance", but I don't fully understand it, I'm sorry...

I think maybe it is because when I do a gettype I'm getting a "type
INSTANCE" of my class ? if so,....

Why it is NOT necessary to put this flag in case of a PUBLIC method with the
same class (changing from "FRIEND" or "PROTECTED sub" to "sub" or "PUBLIC
sub"...

Thanks !
 
J

Jon Skeet [C# MVP]

Roger Tranchez said:
It works, thanks a lot. I searched the web for an explanation of
"BindingFlags.Instance", but I don't fully understand it, I'm sorry...

It's because the method you're looking for is an instance method.
I think maybe it is because when I do a gettype I'm getting a "type
INSTANCE" of my class ? if so,....

Why it is NOT necessary to put this flag in case of a PUBLIC method with the
same class (changing from "FRIEND" or "PROTECTED sub" to "sub" or "PUBLIC
sub"...

When you call the overload of GetMethod which doesn't take a
BindingFlags parameter, it looks for static and instance methods
automatically.
 

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