Determine the load context of an assembly

China☆狼群 提交于 2019-12-03 05:19:48

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: property ReflectionOnly = true
  • no context(dynamic): property IsDynamic = true
  • no context(laod(byteArray): property Location = null
  • default context: either property GlobalAssemblyCache = true or property Location begins with property CodeBase
  • load-from context: anything else assuming you would not load from from code base
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!