Using Reflection

S

Steve

Hi all

If there is a more relevant NG for my question about Reflection I would be
grateful if some-one could point me in the right direction!

I am using the following code to get the Public Methods, Properties and
Events from a particular Assembly, but some extra methods are being listed.
How can I get all the Public Methods, Properties and Events in an Assembly
that I've created?

-------------------------------------------------
Private Sub GetAssemblyDetails()
Dim oAssembly As [Assembly] = [Assembly].LoadFrom(txtPath.Text)
Dim oType As Type
For Each oType In oAssembly.GetTypes
For Each m_oMethod In oType.GetMethods(BindingFlags.Public Or
BindingFlags.Instance Or BindingFlags.DeclaredOnly)
lstMethods.Items.Add(m_oMethod.Name)
Next
For Each m_oProperty In oType.GetProperties(BindingFlags.Public Or
BindingFlags.Instance Or BindingFlags.DeclaredOnly)
lstProperties.Items.Add(m_oProperty.Name)
Next
For Each m_oEvent In oType.GetEvents(BindingFlags.Public Or
BindingFlags.Instance Or BindingFlags.DeclaredOnly)
lstEvents.Items.Add(m_oEvent.Name)
Next
Next
End Sub
----------------------------------------------
The extra things I get are items like get_Version (Version is a property
that is being returned), but also add_InfoMessage (InfoMessage is an Event
that is being returned). How do I eliminate these from my list?

Also, is there a way to just get the information from 1 class. My Assembly
has several classes and will inherit from others, but I just want to return
the information for 1 of the classes.

Thanks in advance.

Regards,
Steve.
 
M

Mattias Sjögren

The extra things I get are items like get_Version (Version is a property
that is being returned), but also add_InfoMessage (InfoMessage is an Event
that is being returned). How do I eliminate these from my list?

You can check the IsSpecialName property on the MethodInfo object. It
will return true for property and event accessors, but also for some
other "special" methods such as operator overloads.

Also, is there a way to just get the information from 1 class. My Assembly
has several classes and will inherit from others, but I just want to return
the information for 1 of the classes.

Then remove the For Each loop and work only with a single Type.



Mattias
 
S

Steve

Hi Mattias

That works a treat!! For the second issue I'm now calling the GetType
function passing the Class name and it works perfectly. Still learning about
reflection and finding it more and more useful all the time :blush:)

Thank you.

Kind Regards,
Steve.
 

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