Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use reflection to reveal the values of global from that are set in the calling assembly.

In the calling assembly I have a main module that specifies many global variables used throughout. I also want to be able to use these in the library dll.

The root namespace of the assembly is "ERP_Generic". The name of the main module is "Main".

The main module: (obviously all this is very simplified for illustration purposes)

Module Main
Public MNApplDir As String = "C:\Program\MyAppDir"
End Module


In the library dll, I have a test sub:

VB
Public Sub Test1()
    Dim mParentAssy As Assembly = Assembly.GetEntryAssembly ' the assembly that called this library class
    Dim moduleType As Type = mParentAssy.GetType("ERP_Generic.Main")
    Dim myFields As FieldInfo() = moduleType.GetFields((BindingFlags.Public Or BindingFlags.Instance))
    Dim mFldName As String = myFields(0).Name
    Dim istr As String = myFields(0).GetValue(mParentAssy)
End Sub


When this sub is called and I step through it, the myFields array does not have any members.

I don't know why the module.GetFields can't find the fields.
Posted

1 solution

I have figured it out. The FieldInfo line has been changed to:

Dim myFields As FieldInfo() = moduleType.GetFields()

And I have added a loop to find the variables I am seeking:

VB
For i As Short = 0 To myFields.Length - 1 Step 1
    If myFields(i).Name = "MNApplDir" Then
        MNApplDir = myFields(i).GetValue(mParentAssy)
    End If
Next i



This works correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900