Reflection from System assemblies

  • Thread starter Thread starter Paul Hatcher
  • Start date Start date
P

Paul Hatcher

Hi

I'm trying to create an instance of an object that's in one of the System
assemblies; this code works fine for classes in my application, but fails
when the assembly is not in the application directory, e.g when it's in the
GAC.

Private Function Create(ByVal className As String) As Object
Dim handle As System.Runtime.Remoting.ObjectHandle

' Now create it
Try
handle = Activator.CreateInstance(AssemblyName, Me.Namespace + "."
+ ClassPrefix + className)
' Unwrap the object and return it
Return CType(handle.Unwrap, Object)
Catch ex As Exception
Throw New Exception("Error occured creating class from " +
Me.Namespace + "." + className + " in " + AssemblyName + " - " +
ex.ToString, ex)
End Try
End Function

Any ideas how to work around this?

Paul
 
Paul,

* "Paul Hatcher said:
I'm trying to create an instance of an object that's in one of the System
assemblies; this code works fine for classes in my application, but fails
when the assembly is not in the application directory, e.g when it's in the
GAC.

The code below worked for me even with Windows Forms's 'Button' class:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///
 
Herfried

Thanks, that worked fine - I started using the version I posted about 2
years ago and it's always worked, but it's been used to create forms
typically.

Paul

Herfried K. Wagner said:
Paul,

* "Paul Hatcher said:
I'm trying to create an instance of an object that's in one of the System
assemblies; this code works fine for classes in my application, but fails
when the assembly is not in the application directory, e.g when it's in the
GAC.

The code below worked for me even with Windows Forms's 'Button' class:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///
 
Back
Top