Assembly.LoadFrom() fails

  • Thread starter Thread starter Michael Maes
  • Start date Start date
M

Michael Maes

Hello,

I want to be able to load an assembly by selecting a dll from an OpenFileDialog.
This seems to work, exept when that assembly references "3rd-Party dll's".

Just this simple line:

Dim myAssembly As [Assembly] = [Assembly].LoadFile(FileName)

throws the following exception:

System.Reflection.ReflectionTypeLoadException: One or more of the types in the assembly unable to load.
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at Stegosoft.AssemblySniffer.Form1.ScanAssembly(String FileName) in ........

Any sugestions?

TIA,


Michael
 
Are these 3rd party dll's .NET assemblies ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Hello,

I want to be able to load an assembly by selecting a dll from an OpenFileDialog.
This seems to work, exept when that assembly references "3rd-Party dll's".

Just this simple line:

Dim myAssembly As [Assembly] = [Assembly].LoadFile(FileName)

throws the following exception:

System.Reflection.ReflectionTypeLoadException: One or more of the types in the assembly unable to load.
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at Stegosoft.AssemblySniffer.Form1.ScanAssembly(String FileName) in ........

Any sugestions?

TIA,


Michael
 
Hi Michael,

Can you use the 3rd-party assembly by referencing them directly instead of
using them by reflection?
Also the 3rd-party assembly may need some dependent module which may cause
the problem.
You may try to use the Fuslogvw.exe tool to see if there is any fusion
error during the assembly binding.
Assembly Binding Log Viewer (Fuslogvw.exe)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/htm
l/cpgrfFusionLogViewerFuslogvwexe.asp


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Terry,

Yes, this happens when a Janus System (v2) Assembly gets involved.
The only thing I want to obtain is scan (a lot) of assemblies to draw an "Object Model".
Lutz Roeder's .NET Reflector can load the assemblies so I guess I'm using the wrong approach....

Regards,

Michael
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message Are these 3rd party dll's .NET assemblies ?

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Hi Peter,

I feel like "an idiot" here...
I've got the tool running, but how do I load an assembly into it?

I guess this is going to be a "blue monday" for me.

B.T.W.: Lutz Roeder's .NET Reflector can load the assemblies so I guess I'm
using the wrong approach....

Regards,

Michael
 
Hi Michael,

To use the tool, we may just run the Fuslogvw.exe, which will show us the
assembly binding information.
You may try to follow the steps in the link below.
Assembly Binding Log Viewer (Fuslogvw.exe)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/htm
l/cpgrfFusionLogViewerFuslogvwexe.asp
You may pay attention to the Other Configuration Settings section.

Also you may try to use the LoadFrom or Load method to see if that help you.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemreflectionassemblyclassloadtopic3.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemReflectionAssemblyClassLoadFromTopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

Indeed the SatteliteDll's don't get loaded.
These are assemblies from which the assembly being scanned is a subclass.

EG:

Assembly A (Parent) & Assembly B (Child)
B inherits from A
If I scan B the LoadFrom-Method fails. (ReflectionTypeLoadException: One or more of the types in the assembly unable to load.)
If I scan A it works fine.

So I guess the LoadFrom-Method doesn't load the SatteliteAssemblies.

Since it can load the Parent-Assembly directly, I don't understand why it can't load it through the derived Assembly.

Any further suggestions?

Thanks,


Michael
 
Reflector does not use System.Reflection. Reflector uses it's own
infrastructure for reading assembly files at the binary level (look at
the Reflector.CodeModel namespace). That might explain why your file
loads in Reflector but not using Assembly.LoadFrom().
 
Hi Michael,

Based on my test, it seems that if the inherited class in not in the same
assembly of parent class, we can load the assembly.
Here is my test result.
[ParentClass]
Public Class PC
Public mem As Integer
Public Sub New()
mem = 1
End Sub
Public Sub Test()
Console.WriteLine(mem)
End Sub
End Class

[ChildrenClass]
Public Class ChildClass
Inherits ParentClass.PC
Public Sub New()
Me.mem = 2
End Sub
Public Sub T()
Me.Test()
End Sub
End Class

Imports System.Reflection
Module Module1
Sub Main()
Dim ass As [Assembly] =
[Assembly].LoadFrom("<path>\ChildrenClass.dll")
Dim t As Type = ass.GetType("ChildrenClass.ChildClass")
Dim o As Object = Activator.CreateInstance(t,
BindingFlags.CreateInstance, Nothing, New Object() {}, Nothing, Nothing)
t.InvokeMember("T", BindingFlags.InvokeMethod, Nothing, o, New
Object() {}, Nothing, Nothing, Nothing)
End Sub
End Module

You may try the code above to see if that help you.

As for your scnario, can you send me the a simple reproduce sample by
removing the "Online" from my email address with detailed reproduce steps.
Also have you tried my suggestion that use the load method instead, did
that work for you?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Michael,

It seems that the problem is caused by the difference between loadfile and
loadfrom. For this scenario, the loadfrom will be a proper method.
Remarks
Use the LoadFile method to load and examine assemblies that have the same
identity, but are located in different paths. Do not use LoadFile to load
assemblies that you want to execute. LoadFile does not load files into the
LoadFrom context, and does not resolve dependencies using the load path, as
the LoadFrom method does. LoadFile is useful in this limited scenario
because LoadFrom cannot be used to load assemblies that have the same
identities but different paths; it will load only the first such assembly.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemreflectionassemblyclassloadfiletopic1.asp

If we change the loadfile to loadfrom in your reproduce sample, it will
solve the problem, you may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

It does seem to solve the issue.
Thanks for your time (once again).

Kind regards,

Michael
 
Hi Andre,

Thanks for your input.
You "unintendedly" brought my attention to some Reflector Plug-Inns, ...,
wow!

Kind regards,

Michael

BTW: The issue has been solved :-) so I'm (twice) glad!
 
Back
Top