问题
I want to compare two identical .net dlls which are located at different locations. Hence, I am loading the dlls using System.Reflection.Assembly.LoadFile(filename)
instead of System.Reflection.Assembly.LoadFrom(filename)
. But the .Net dlls that are to be compared have reference to other assemblies (which are in the same folder as the respective dll). Loading the dll using LoadFile(filename)
followed by GetTypes()
throws an ReflectionTypeLoadException
. How should I load two identical dlls using reflection so as to get their types & compare?
回答1:
Use the ReflectionOnlyLoad ot ReflectionOnlyLoadFrom methods
You will also need to handle the ReflectionOnlyAssemblyResolve to tell the framework where to find the dependencies.
回答2:
You can catch the ReflectionTypeLoadException
in order to see the list of types which were loaded correctly. See the following links for some more detail on this:
- How do i get a list of classes in .NET
- MissingMethodException thrown by GetExportedTypes
If you know where the missing assemblies can be found you can also handle the AppDomain.AssemblyResolve event in order to "help out" with locating and loading the required dependencies.
回答3:
If you just want to compare two assemblies, you certainly doesn't need to load them. I would suggest a more static approach, based on Mono.Cecil.
It will allow you to compare modules, types, methods -- even at the instruction level, in a simple manner.
Getting started documentation can be found here :
https://github.com/jbevain/cecil/wiki/HOWTO
来源:https://stackoverflow.com/questions/6648577/c-sharp-comparing-two-net-dlls-using-reflection