Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

c# - How to check programmatically whether a managed assembly is x86, x64 or AnyCPU?

I need to determine programmatically whether an assembly is x86, x64 or AnyCPU? There is an almost identical question, but the solution that it provides

Assembly assembly = Assembly.LoadFrom(fileName);
PortableExecutableKinds peKind;
ImageFileMachine imageFileMachine;
assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine);

fails when trying to load a 64-bit assembly from a 32-bit process (and vice versa).

Is there a foolproof way of programmatically finding out the compilation type of an assembly?

EDIT: Based on @BenVoigt suggestion, I created a small command line utility that checks whether the DLL is managed or not and whether its x86/x64/AnyCPU. I hope someone finds it useful.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This question's been covered already:

But the answers are incomplete, suggesting use of Assembly.LoadFrom. That's a terrible idea, since it will run code from the assembly, in addition to failing if the bitness doesn't match your process.

Instead, you should use Assembly.ReflectionOnlyLoadFrom. This lets you read the metadata without actually loading any code, and therefore there's no need for the architecture to be correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...