views:

389

answers:

3

I need to write a CorFlags-like application. If I have a path to assembly file, how to I read its CorFlags?

I specifically need to know if the assembly is Any-CPU or x86 only

I want to avoid loading the assembly using reflection because I need to scan many files.

ThanksSaa

+4  A: 

Hi there.

Have a look at this link, it describes a similar situation to your query. The accepted answer describes how you can use reflection and Module.GetPEKind. This might help you.

Edit: A bit late, but here's a example:

namespace ConsoleApplication1
{
    using System.Reflection;

    public class Program
    {
        public static void Main()
        {
            Assembly a = Assembly.GetExecutingAssembly();

            PortableExecutableKinds peKind;
            ImageFileMachine machine;

            a.ManifestModule.GetPEKind(out peKind, out machine);
        }
    }
}

Cheers. Jas.

Jason Evans
+2  A: 

System.Reflection.Assembly has a whole bunch of things to help you in this.

var asmbly = Assembly.ReflectionOnlyLoad("filename");
var PeFlags = asmbly.ManifestModule.GetPEKind();
//PeFlags is effectively CorFlags.
Spence
+1  A: 

If you don't want to use reflection, you may check out

CCI http://ccimetadata.codeplex.com/Wiki/View.aspx?title=API%20overview or

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

Lex Li
CCI worked great. Much faster then using reflection to load all the assemblies
Saar
Better performance is expected, as CCI simply reads metadata from assemblies directly. Reflection will load the whole assemblies which is doomed to be much slower. Luckily CCI has been open sourced and released publicly. :)
Lex Li