在C#中验证.NET程序集?

Euc*_*lid 4 c# reflection dll exception

如何检查加载的程序集是否是有效的.NET程序集?我目前有这个代码,但非托管DLL抛出BadImageFormatException.

 string[] filepaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll", SearchOption.AllDirectories);
        List<Type> potentialEffects = new List<Type>();
        foreach (string filepath in filepaths)
        {
            Assembly a = Assembly.LoadFile(filepath);
            potentialEffects.AddRange(a.GetTypes());
        }
Run Code Online (Sandbox Code Playgroud)

Seb*_*ter 11

你可以简单地抓住这个例外吗?

另请参阅此文章:http://blogs.msdn.com/suzcook/archive/2004/03/17/determining-whether-a-file-is-an-assembly.aspx 抓住异常.检查比假设文件是​​有效程序集要昂贵得多.

  • 欧几里德:你是微观优化的.与实际加载相比,抛出'BadImageFormatException`的成本是微不足道的. (8认同)