Common Language Runtime
Assembly.GetTypes() does not work
Question:
Hi,
I am using reflection to load a dll and I am getting the following exception when I try to get the Assembly types using .GetTypes(). Please advice!
System.Reflection.ReflectionTypeLoadException was unhandled
Message="Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
Source="mscorlib"
StackTrace:
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at ReflectAssembly.Class1.Main(String[] args) in C:\Documents and Settings\aalamo\Desktop\ReflectAssembly\Class1.cs:line 48
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Answer1:
I guess assembly (assembly 1) you're trying to reflect to get types references types defined in the another assembly (assembly 2).
When you're trying to get a list of types defined in assembly 1 it (for some nasty reason) fails to load assembly 2 and stops with the exception you'd posted.
Follow the advice given by MS developer and examine LoaderExceptions property of exception to find out that went wrong.
http://sukhov.net
Answer2:
"examine LoaderExceptions property of exception to find out that went wrong." that is what the exception says. Thank you very much for pointing that out. I could not have known that on my own.
Any real solution to this problem.? Some of us really want to know!!
Answer3:
Try the following code:-
Public Shared Function GetTypes(ByVal objAssembly As System.Reflection.Assembly) As Type()
Dim objTypes() As Type = Nothing
Try
'In a medium trust environment this call will fail.
'We then just need to get the types that were loaded.
'Thankfully these can be got from the exception.
objTypes = objAssembly.GetTypes()
Catch ex As Reflection.ReflectionTypeLoadException
objTypes = ex.Types
#
If DEBUG Then
For Each ex1 As Exception In ex.LoaderExceptions
System.Diagnostics.Debug.WriteLine(
"--LoaderExceptions: " + ex1.Message)
Next
#
End If
End Try
Return objTypes
End Function
Answer4:
Hi,
I have excatly the same problem.
My LoaderException is Null.
Did'you find any solution ?
Thanks.