为什么reflection.assembly loadfile不加载所有dll?

时间:2010-07-18 08:06:39

标签: c# reflection assemblies

我正在使用LoadFrom()来加载dll,但由于某种原因,这个加载函数不能在所有dll上运行, 我想加载3000个dll来获取每个版权属性。

我的代码:

    class ReverseDLL
{
    private Assembly assembly;
    private AssemblyDescriptionAttribute desc;
    private AssemblyTitleAttribute title;
    private AssemblyCopyrightAttribute copyRight;

    public string getCopyright(string path)
    {
        try
        {
            //assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path));
            assembly = System.Reflection.Assembly.LoadFrom(path);//"C:\\Windows\\winsxs\\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\\msvcm90d.dll");//path);// LoadFrom(path);

                desc = (AssemblyDescriptionAttribute)
                AssemblyDescriptionAttribute.GetCustomAttribute(
                assembly, typeof(AssemblyDescriptionAttribute));

                title = (AssemblyTitleAttribute)
                AssemblyTitleAttribute.GetCustomAttribute(
                assembly, typeof(AssemblyTitleAttribute));

                copyRight = (AssemblyCopyrightAttribute)AssemblyCopyrightAttribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute));
        }
        catch
        {
              this.copyRight = new AssemblyCopyrightAttribute("");
        }

        if (this.copyRight == null)
            this.copyRight = new AssemblyCopyrightAttribute("");

        return copyRight.Copyright;
    }
}

3 个答案:

答案 0 :(得分:4)

如果没有提供更多信息(例如错误),我不知道反射问题,但 也可以尝试访问文件本身:

string copyright = FileVersionInfo.GetVersionInfo(path).LegalCopyright;

这可以访问文件系统元数据(就像你在资源管理器中看到的那样),并且具有为托管和非托管dll工作的优势。 它要求存在元数据(查看属性)。

编辑:快速检查表明(正如预期的那样)编译器 检查此属性并正确填充文件元数据。

答案 1 :(得分:0)

您是否尝试过停止异常? Ctrl + Alt + E,在抛出框架异常时停止。异常消息应该为您提供有关无法加载DLL的原因的信息。

答案 2 :(得分:0)

使用反射不是最佳方法,因为某些dll可能具有您没有的依赖项。

使用元数据解析器可以为您提供所需的内容,

http://ccimetadata.codeplex.com/

http://www.mono-project.com/Cecil

Marc提到的方式不适用于大多数特定于.NET的元数据。