D
Dominique Vandensteen
I want to dynamicly load a type (typename is defined in the database).
This type is located in the exe itself or one of the dll's in the directory
with the exe file.
When creating an instance directly it works, when using reflection none of
the dll's is "scanned".
I used following code to work around this problem.
Now I just want to know if this is the/a good way to do it, or should I use
another system.
Private Shared _assemblies As System.Reflection.Assembly() = Nothing
Private Shared Function MyGetType(ByVal typeName As String) As Type
Dim result As Type = Type.GetType(typeName)
If (Not result Is Nothing) Then Return result
If (_assemblies Is Nothing) Then
Dim dir As String =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutableP
ath)
Dim dlls() As String = System.IO.Directory.GetFiles(dir, "*.dll")
ReDim _assemblies(dlls.Length - 1)
For i As Integer = 0 To dlls.Length - 1
_assemblies(i) = System.Reflection.Assembly.LoadFile(dlls(i))
Next i
End If
For Each asm As System.Reflection.Assembly In _assemblies
result = asm.GetType(typeName)
If (Not result Is Nothing) Then Return result
Next asm
Return Nothing
End Function
This type is located in the exe itself or one of the dll's in the directory
with the exe file.
When creating an instance directly it works, when using reflection none of
the dll's is "scanned".
I used following code to work around this problem.
Now I just want to know if this is the/a good way to do it, or should I use
another system.
Private Shared _assemblies As System.Reflection.Assembly() = Nothing
Private Shared Function MyGetType(ByVal typeName As String) As Type
Dim result As Type = Type.GetType(typeName)
If (Not result Is Nothing) Then Return result
If (_assemblies Is Nothing) Then
Dim dir As String =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutableP
ath)
Dim dlls() As String = System.IO.Directory.GetFiles(dir, "*.dll")
ReDim _assemblies(dlls.Length - 1)
For i As Integer = 0 To dlls.Length - 1
_assemblies(i) = System.Reflection.Assembly.LoadFile(dlls(i))
Next i
End If
For Each asm As System.Reflection.Assembly In _assemblies
result = asm.GetType(typeName)
If (Not result Is Nothing) Then Return result
Next asm
Return Nothing
End Function