Link to home
Create AccountLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Using Reflections to get Form.TAG

Hi, I have a snippet of code (attached below) that was provided to me by"TheLearnedOne." The code works VERY well, in that it provides me the Form "NAME" of the Forms within a DLL. What I was trying to do is modify it to provide me the Form.TAG property of each Form as well. That way I can add a specific TAG Name to my Forms withing the DLL and check for them at runtime.

I was trying to use the "currentType.GetProperty()" method, but it doesn't seem to work for me. Or at least, I may not be setting it up correctly.

This is the code I was using, which doesn't work ....!!!
For Each currentType As Type In assy.GetTypes()
sFormTag = currentType.GetProperty(currentType.Name.Tag)  <<< Not good
    Debug.WriteLine(sFormTag)
Exit For

Any ideas of how I can enumerate the TAG property of a Form from within a DLL using Reflections?

Thanks,
Fulano
Imports System
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Windows.Forms
 
Public Class ApplicationForms
   Public Function EnumerateForms() As List(Of String)
     Dim assy As Assembly = Assembly.GetEntryAssembly()
     Dim list As New List(Of String)()
     For Each currentType As Type In assy.GetTypes()
       If currentType.BaseType.Equals(GetType(Form)) Then
        list.Add(currentType.Name)
      End If
    Next
     Return list
   End Function
 End Class

Open in new window

Avatar of roshkins
roshkins

Although I don't know much about reflections, there seems to be an error. Try changing this:

For Each currentType As Type In assy.GetTypes()
sFormTag = currentType.GetProperty(currentType.Name.Tag)  <<< Not good
    Debug.WriteLine(sFormTag)
Exit For

to:
For Each currentType As Type In assy.GetTypes()
sFormTag = currentType.GetProperty(currentType.Tag) 
    Debug.WriteLine(sFormTag)
Exit For
 
'Hope this helps.

Open in new window

Or try:
For Each currentType As Type In assy.GetTypes()
sFormTag = currentType.Tag
    Debug.WriteLine(sFormTag)
Exit For

Open in new window

To get the value of the Tag property you need to use PropertyInfo.GetValue. To use that, you need to pass an instance of a Form object to it. According to the code you posted, it doesn't look like you have any instances to examine.
Avatar of Mr_Fulano

ASKER

Hi ZachSmith, can you elaborate on that a little bit for me. Maybe an example of how I would pass the instance of the Form Object to PropertyInfo.GetValue.

Any help would be greatly appreciated!

Thanks,
Fulano
Ok, say you have a Form instance called form1.. The code to get the Tag property would be:

Dim formType As Type = form1.GetType()
 
Dim property As PropertyInfo = formType.GetProperty("Tag")
 
Dim sTag As String = property.GetValue(form1, Nothing).ToString()
 
Console.WriteLine(sTag)

Open in new window

Notice that in the code above I am passing an instance of a Form (form1) to the GetValue method
I'll try that, but I'm wondering if it will work given that the Form I'm trying to get is in the DLL, which is NOT part of this project. The DLL is an external Form in a separate project. -- That's why I have to use Reflections. However, I'll give this a try.

Fulano
As long as you have access to the form it should work. If you keep having trouble post up the complete code and I'll try to help you in the morning.
Hi ZackSmith, here is the code. I'm getting the following error:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."

Any thoughts?  
============================================
Private Function EnumerateDLL(ByVal path As String) As String
Dim ra As Reflection.Assembly = Reflection.Assembly.LoadFrom(OpenFileDialog1.FileName)
        Dim types() As Type = ra.GetTypes()
        Dim strFormName As String = ""
        Dim bolFindForm As Boolean = True
        '
        For Each currentType As Type In ra.GetTypes()
            If currentType.BaseType.Equals(GetType(Form)) Then
                strFormName = currentType.Name
                Debug.WriteLine("The Form Name is  :" & currentType.Name)
                '========= TEST CODE FOT TAG ==================================
                Dim formType As Type = strFormName.GetType()
                Dim myProperty As PropertyInfo = formType.GetProperty("Tag")
                Dim sTag As String = myProperty.GetValue(strFormName, Nothing).ToString()
                Debug.WriteLine("The TAG Name is  :" & sTag)
                '==============================================================
                Exit For
            End If
        Next
        Return strFormName
    End Function
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
OK...with a tiny bit of tweaking, its working! -- This is GREAT!!!

Private Function EnumerateDLL(ByVal path As String) As String
        Dim ra As Reflection.Assembly
        If Not OpenFileDialog1.FileName Is DBNull.Value Then
            ra = Reflection.Assembly.LoadFrom(OpenFileDialog1.FileName)
        Else
            Return String.Empty
            Exit Function
        End If
        Dim types() As Type = ra.GetTypes()
        Dim strFormName As String = ""
        Dim bolFindForm As Boolean = True
        '
        For Each currentType As Type In ra.GetTypes()
            If currentType.BaseType.Equals(GetType(Form)) Then
                strFormName = currentType.Name
                Debug.WriteLine("The Form Name is  :" & currentType.Name)
                '========= TEST CODE FOT TAG ==================================
                Dim myForm As Form = CType(Activator.CreateInstance(currentType), Form)
                Dim myProperty As PropertyInfo = currentType.GetProperty("Tag")
                Debug.WriteLine("myForm = :" + myForm.ToString)
                Dim sTag As String = myProperty.GetValue(myForm, Nothing).ToString()
                Debug.WriteLine("The TAG Name is  :" & sTag)
                '==============================================================
                Exit For
            End If
        Next
        Return strFormName
    End Function
Fantastic work...!!!