zoukankan      html  css  js  c++  java
  • .net反射解析dll时 关联dll的加载

    问题:

    一个解析器:

    using System;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Text;

    classProgram
    {
       
    staticvoid Main(string[] args)
        {
           
    string filename = args[0];
           
    Assembly a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(filename);

           
    Console.WriteLine("Opened assembly:{0}", filename);
           
    foreach (Type t in a.GetTypes())
            {
               
    Console.WriteLine("  " + t.FullName);
            }
        }
    }

    使用:

    type_sniff.exe type_sniff.exe
    Openedassembly:type_sniff.exe
      Program

      但是当dll中有关联时,如:
    // defined in a.dll
    publicclass Foo
    {
       
    public Foo()
        {
        }
    }

    // defined in b.dll, compiled as /r:a.dll
    publicclass Bar : Foo
    {
        Bar()
        {
        }
    }

    运行: type_sniff.exe b.dll

    C:\bug\type_sniff\type_sniff\bin\Debug>type_sniff.exe b.dll
    Opened assembly:b.dll
    Unhandled Exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
    at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
    at System.Reflection.Assembly.GetTypes()
    at Program.Main(String[] args) 
    Exception 的message为:

    {"Cannot resolve dependency to assembly 'a, Version=2.1.0.0, Culture=neutral, PublicKeyToken=ebb8d478f63174c0'

    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.":"a, Version=2.1.0.0, Culture=neutral, PublicKeyToken=ebb8d478f63174c0"}

    解决方法:

    ReflectionOnlyAssemblyResolve event时加入需要加载的dll进来, 这里假设dll都在working directory

    using System;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Text;

    classProgram
    {
       
    staticvoid Main(string[] args)
        {
           
    string filename = args[0];
           
    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += newResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
           
    Assembly a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(filename);

           
    Console.WriteLine("Opened assembly:{0}", filename);
           
    foreach (Type t in a.GetTypes())
            {
               
    Console.WriteLine("  " + t.FullName);
            }
        }

       
    staticAssembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
        {
           
    return System.Reflection.Assembly.ReflectionOnlyLoad(args.Name);
        }
    }


  • 相关阅读:
    传Windows 7 RC泄露版中含有木马 狼人:
    金山:3G时代 上网安全面临更大挑战 狼人:
    McAfee:僵尸网新威胁远甚Conficker 狼人:
    安全专家:70GB财务数据被僵尸网络盗窃 狼人:
    卡巴斯基中国地区4月恶意软件排行榜 狼人:
    MPAA组织遭遇尴尬 网页存在XSS攻击漏洞 狼人:
    股市回暖 网上炒股安全风险骤增 狼人:
    微软首次针对Windows 7推杀毒软件 年内将推出 狼人:
    黑客数度入侵美国联邦航空总署飞航控制系统 狼人:
    瑞星对Windows7捆绑杀毒软件等消息的回应 狼人:
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1321203.html
Copyright © 2011-2022 走看看