Fully Qualified Class Name via Reflection - Namespace.Namspace.ClassName

J

Jeff Molby

Sorry for the crossposting guys. I figured this one might be a little
tricky, so I'm calling upon the C# brains out there to help out this poor VB
developer. I know enough C# to translate any code you can offer, if
necessary.

I have a very loosely coupled WinForms app written in VB.Net, Framework v1.1

I need to take a string value which contains the unqualified name of a
class, find the assembly that contains such a class, and instantiate the
class.

Here is my code to find the correct assembly.

Private Function GetAssembly(ByVal ForeignTable As String) As
Reflection.Assembly
Dim Assems() As Reflection.Assembly
Dim intLCV As Integer

Assems = AppDomain.CurrentDomain.GetAssemblies

For intLCV = 0 To Assems.Length - 1
If Not Assems(intLCV).GetType(ForeignTable, False, True) Is Nothing
Then
Return Assems(intLCV)
End If
Next

Return Nothing
End Function

It works fine, except my IF statement always returns Nothing because the
ForeignTable value is unqualified. If I hardcode it to
"BookWorks.InventoryBO.InventoryItems", it works just fine, but it fails on
"InventoryItems". It appears that I need find the namespace that covers the
assembly and concatenate that to ForeignTable. I can't find any
documentation on how to do this.

Any ideas?

Thanks everyone!
 
J

Jeff Molby

Nevermind, I found a workaround. I added the following function and reworked
the GetAssembly() function to instantiate the return value.

Private Function AssemblyHasType(ByRef Assem As Reflection.Assembly,
ByVal ForeignTable As String) As String
Dim x As Type

For Each x In Assem.GetTypes
If x.Name = ForeignTable Then
Return x.FullName
End If
Next

Return ""
End Function
 

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