简体   繁体   English

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

[英]why reflection.assembly loadfile doesn't load all dlls?

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. 我正在使用LoadFrom()来加载dll,但是由于某种原因,此加载功能不适用于所有dll,我想加载3000个dll以从每个版权属性中获取。

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

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; 这样可以访问文件系统元数据(就像您在资源管理器中看到的那样),并且具有可以同时处理托管和非托管dll的优点。 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. 编辑:快速检查表明(按预期)编译器确实检查了此属性并正确填充了文件元数据。

Have you tried stopping on exceptions? 您是否尝试过停止例外? Ctrl + Alt + E, stop on framework exceptions when they are thrown. Ctrl + Alt + E,在引发框架异常时停止它们。 The exception message should give you some information as to why the DLL couldn't be loaded. 异常消息应为您提供一些有关为何无法加载DLL的信息。

Using reflection is not the optimal approach, as some of the dll may have dependencies you don't have. 使用反射不是最佳方法,因为某些dll可能具有您没有的依赖项。

Using a metadata parser can give you the things you want, 使用元数据解析器可以为您提供所需的内容,

http://ccimetadata.codeplex.com/ http://ccimetadata.codeplex.com/

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

The way Marc mentioned does not work for most .NET specific metadata. Marc提到的方法不适用于大多数.NET特定的元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM