Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with ReflectionOnly load context

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?

like image 677
lysergic-acid Avatar asked Aug 07 '26 15:08

lysergic-acid


1 Answers

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.

like image 108
hshen Avatar answered Aug 10 '26 11:08

hshen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!