为什么 Reflection.Assembly LoadFile 不加载所有 DLL?

发布于 2024-09-10 16:12:09 字数 1462 浏览 2 评论 0原文

我正在使用 LoadFrom() 来加载 dll,但由于某种原因,此加载函数不适用于所有 dll, 我想加载 3000 个 dll,以便从每个 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;
    }
}

I am using LoadFrom(), to load dlls, but for some reason this load function doesn't work on all dlls,
i want to load 3000 dlls to get from each one the copyright attribute.

my code :

    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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-09-17 16:12:09

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

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

这会访问文件系统元数据(就像您一样)会在资源管理器中看到),并且具有适用于托管和非托管 dll 的优点; 但是它要求元数据存在(它查看属性)。

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

I don't know about the reflection problem without you providing more info (such as the error), but you could also try access the file itself:

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

This accesses the file-system meta-data (like you would see in explorer), and has the advantage of working for both managed and unmanaged dlls; but it requires that meta-data to exist (it doesn't look at the attribute).

Edit: a quick check indicates that (as expected) the compiler does check for this attribute and populate the file meta-data correctly.

如若梦似彩虹 2024-09-17 16:12:09

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

Have you tried stopping on exceptions? Ctrl + Alt + E, stop on framework exceptions when they are thrown. The exception message should give you some information as to why the DLL couldn't be loaded.

暗地喜欢 2024-09-17 16:12:09

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

使用元数据解析器可以为您提供您想要的东西,

http://ccimetadata.codeplex.com/

< a href="http://www.mono-project.com/Cecil" rel="nofollow noreferrer">http://www.mono-project.com/Cecil

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

Using reflection is not the optimal approach, as some of the dll may have dependencies you don't have.

Using a metadata parser can give you the things you want,

http://ccimetadata.codeplex.com/

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

The way Marc mentioned does not work for most .NET specific metadata.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文