lundi 18 novembre 2019

How to ensure typeof and cast operators operate on a different load context not the default load context

I'm using .NET core and trying to compare types loaded via reflection into an assembly load context (not the default load context) with a known compile-time type. However, if I use the typeof operator like so:

if(x.GetType() == typeof(KnownType))

The result is always false even when all the properties of the two types (including the location they were loaded from is the same), presumably because they are loaded into different load contexts... the custom context and the default context.

I can get this to work by pre-loading the known type or interface into the custom context, getting a reference to it and doing the comparison like so:

if(x.GetType() == preloadedKnownType)

However, I have a similar situation with casts. If I do:

var y = x as KnownType

the cast fails, presumably because they are loaded into different contexts.

Is there a way of using the typeof and cast operators with a type loaded into a custom context?





Aucun commentaire:

Enregistrer un commentaire