Determine the load context of an assembly
Instead of identifying the context of the assembly, you could test the behavior of it. For example, for serializing, the serializer will call Assembly.Load and that assembly must match the assembly of the object being serialized. A match can be tested for by checking the CodeBase.
private static bool DoesAssemblyMatchLoad(Assembly assemblyToTest)
{
try
{
var loadedAssembly = Assembly.Load(assemblyToTest.FullName);
return assemblyToTest.CodeBase == loadedAssembly.CodeBase;
}
catch (FileNotFoundException)
{
return false;
}
}
reflection-only context:
Assembly.ReflectionOnly == true
no context (dynamic):
Assembly.IsDynamic == true
no context (
Load(byte[])
):Assembly.Location == null
default context: either
Assembly.GlobalAssemblyCache == true
orAssembly.Location
begins with property CodeBaseload-from context: anything else assuming you would not load from from code base