2
votes

I am trying to speed up my app by loading certain DLLs into the ReflectionOnly context.

This loading occurs on a secondary AppDomain, which registers both AssemblyResolve and AssemblyReflectionOnlyResolve event handlers.

For some reason, when loading these assemblies, the code fails on this method:

Type[] tps = dll.GetTypes();

This throws an exception saying:

Cannot resolve dependency to assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.

When loading the assemblies not into the Reflection Only context, this works as assumed.

Are there any gotchas/caveats for using the Reflection Only context? why doesn't the runtime able to find this assembly in the GAC and load it as usual? am i missing something?

1

1 Answers

7
votes

From MSDN: "Dependencies are not automatically loaded into the reflection-only context". So in your AppDomain.ReflectionOnlyResolve event handler, you have to load 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' by Assembly.ReflectionOnlyLoad(). You should get the depended assembly name from Name property of ResolveEventArgs as

 public static Assembly My_AssemblyResolve(object sender, ResolveEventArgs args) 
 {
      string missedAssemblyFullName = args.Name;
      Assembly assembly = Assembly.ReflectionOnlyLoad(missedAssemblyFullName); 
      return assembly
 }

Note: ReflectionOnlyLoad() loads only the assemblies in GAC. You may use ReflectionOnlyLoadFrom to load dll directly.