We created a small component that takes an identifier, looks at the database record for the assembly / namespace / class, and dynamically loads the instance of the class that we need. It still works, but when you run this code in VS 2010, it does not work.
Private Function AssemblyLoaded(ByVal assemblyFile As String) As Assembly Dim assemblies() As Assembly = AppDomain.CurrentDomain.GetAssemblies For Each asmb As Assembly In assemblies If (asmb.Location = assemblyFile)) Then Return asmb Next Return Nothing End Function
The first problem is that when an iterator hits the dynamic assembly, there is no asmb.Location and a NotSupportedException. Is there a way to check the unsupported location field without getting an exception?
The second problem: asmb.Location returns the whole path, not just the file name, which means that this function is interrupted every time. If this function determines that the class has not yet been loaded, we try to load it and get an AccessViolationException because the class is already loaded and we cannot "reload" it.
Function change for this:
Private Function AssemblyLoaded(ByVal assemblyFile As String) As Assembly Dim assemblies() As Assembly = AppDomain.CurrentDomain.GetAssemblies For Each asmb As Assembly In assemblies Try If (asmb.Location.EndsWith(assemblyFile)) Then Return asmb Catch ex As NotSupportedException Continue For End Try Next Return Nothing End Function
But he feels dirty. Is there a better way to check if the assembly is already loaded and pass it back to the caller? Are the problems above specific for .NET 4.0 or Visual Studio 2010? I have not tried this outside of the IDE, as this requires a fairly significant configuration.
Josh Smeaton
source share