Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.ReflectionOnlyLoadFrom not working

I have an Assembly Library1.dll which contains some Interfaces, which were serialized as a byte array into the database. For some reasons we have to change the Interface properties and defintion. so now i am writing a migration utility. So i have 2 versions of Library1.dll , In my utility i have created a folder where i store the new version of Library1.dll . This utility in turn also references Library1.dll hence in bin folder contains Library1.dll but this dll is compiled on older version. My new version of Library1.dll is stored in a private path which i am passing to Assembly.ReflectionOnlyLoadFrom function to instantiate and hence GetTypes on the assembly loaded which further would enable me to do conversion of data.

But I always get ReflectionTypeLoadException when trying to load Library1.dll from private path.

Please help guys!!!. any help would be appreciated. I am really stuck.

Thanks, AG

like image 375
netmatrix01 Avatar asked Feb 23 '10 20:02

netmatrix01


People also ask

Why is reflectiononlyload not Loading my Assembly?

If the assembly has dependencies, the ReflectionOnlyLoad method does not load them. If you need to examine them, you must load them yourself. Determine whether an assembly is loaded into the reflection-only context by using the assembly's ReflectionOnly property.

What is the use of reflection in assembly language?

The attribute is applied to the assembly, to a type declared in the assembly, to a method of the type, and to a parameter of the method. When executed, the assembly loads itself into the reflection-only context and displays information about the custom attributes that were applied to it and to the types and members it contains.

Is the same assembly loaded in both execution context and reflection-only context?

Normally, you would not expect to find the same assembly loaded into both the execution context and the reflection-only context.

How do I examine an assembly in Java?

If you need to examine them, you must load them yourself. Use the ReflectionOnlyLoad (String) method overload to load the assembly given its display name, or the ReflectionOnlyLoadFrom method to load the assembly given its path. If the assembly is a binary image, use the ReflectionOnlyLoad (Byte []) method overload.


1 Answers

If your Library is referencing another dll, GetTypes will fail when it hits a type that uses an external type. Unlike normal assembly loading, ReflectionOnly Assembly loading will not resolve dependencies. You can either subscribe to AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve and load the dependencies as required, or you could pre-load them.

This is the code I use for this:

var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
foreach (var assemblyName in assembly.GetReferencedAssemblies()) {
  try {
    Assembly.ReflectionOnlyLoad(assemblyName.FullName);
  } catch {
    Assembly.ReflectionOnlyLoadFrom(Path.Combine(Path.GetDirectoryName(assemblyPath), assemblyName.Name + ".dll"));
  }
}

This will try to load all dependencies of the reflection-only loaded assembly first by fullname, then by path (assuming that the dependency is in the same directory as the loaded assembly).

like image 108
Arne Claassen Avatar answered Oct 20 '22 13:10

Arne Claassen