Reflecting methods

B

billsahiker

How can I use reflection to inspect any assembly and get only the
methods written by the developers of the assembly, not all the built-
in methods. When I use GetMethods on the Module, it returns all the
built-in methods, so for a Form, I get
get_autosize, get_autoscroll, which I do not want. How can I filter
those out? I read the documentation and looked into bindingflags and
tried many other options but cannot find anything that does what I
need. Using framework 2.0 and vs2005. Please excuse if this is posted
twice -I got a google server error first time, so am sending again.
 
M

Mattias Sjögren

How can I use reflection to inspect any assembly and get only the
methods written by the developers of the assembly, not all the built-
in methods. When I use GetMethods on the Module, it returns all the
built-in methods, so for a Form, I get
get_autosize, get_autoscroll, which I do not want. How can I filter
those out?

BindingFlags.DeclaredOnly is the flag you should be using.


Mattias
 
B

billsahiker

I can get all the types in the assembly, but using the declaredonly
flag returns an empty memberInfo array. I tried the following code on
several projects, each with one form and one class. A button_click in
the form creates an instance of the public class and calls one of its
public methods. These projects compile and run without error.

Dim a As Assembly = Assembly.LoadFrom(Path)
Dim alltypes as Type = a.GetTypes
For each t As Type in alltypes
lstTypes.Items.Add(t.Name)
Dim mems() as MemberInfo = t.GetMembers(BindingFlags.DeclaredOnly)
For each m as MemberInfo in mems
lstMethods.Items.Add(m.Name)
Next
Next
 
M

Mattias Sjögren

I can get all the types in the assembly, but using the declaredonly
flag returns an empty memberInfo array.

Specifying only DeclaredOnly is not sufficient. You have to combine it
with other BindingFlags to say exactly what you want (Public,
NonPublic, Static, Instance etc).


Mattias
 

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