2

Until .net 6 I couldl get the processor architecture (eg., MSIL, Amd64) of the current assembly with:

Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture

But is obsolete since .net7.

It was suggested to use something from the namespace System.Reflection.Metadata. However it's not clear how to get ProcessorArchitecture.

How to get this piece of information ? preferably without having to give an assembly name.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
Soleil
  • 6,404
  • 5
  • 41
  • 61

2 Answers2

2

You can get this from the Module information, which represents the actual DLL. It has a GetPEKind function which returns information on which architecture it is compiled for.

Assembly.GetExecutingAssembly().Modules.First().GetPEKind(out var pekind, out var machine);
Console.WriteLine(pekind);
Console.WriteLine(machine);

dotnetfiddle

Note that theoretically an assembly can have multiple modules, although this is unusual.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
1

Something similar is from PE file header,

using System;
using System.IO;
using System.Reflection.PortableExecutable;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using var fs = new FileStream(@"Example.dll", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using var peReader = new PEReader(fs);
            Machine machine = peReader.PEHeaders.CoffHeader.Machine;
            Console.WriteLine(machine);
        }
    }
}

The values of Machine can be found in this reference.

Lex Li
  • 60,503
  • 9
  • 116
  • 147