Live Chat Programs

March 25, 2008

Determining Whether a File Is an Assembly

Filed under: Live Chat software

Also see: From C# to Java: Part 3

A file is an assembly if and only if it’s managed and it contains an Assembly entry in its CLR metadata.

Determining by hand

A fast way to determine whether a file is an assembly is to run ildasm.exe on it. If it immediately gives an error saying that it may not be a PE file, then it’s not a managed file. But, if it is an assembly, then ildasm will show an entry for the Assembly definition (“.assembly“ in the MANIFEST window or at the bottom of the original window).

Determining programmatically

From unmanaged code, you can call GetAssemblyFromScope() on the IMetaDataAssemblyImport interface for the file. If it returns S_OK, it’s an assembly. If it returns CLDB_E_RECORD_NOTFOUND, it’s not an assembly.

From managed code, if AssemblyName.GetAssemblyName(), Assembly.Load*(), etc. succeeds when given that file, then it’s an assembly. If the load failed with a BadImageFormatException, then it may not be an assembly. There are other reasons, however, why that exception may have been thrown (maybe it’s an assembly but could not be loaded because it has an incorrect format). Coming soon in v2, if the hresult for BadImageFormatException is COR_E_ASSEMBLYEXPECTED, then it’s because it’s not an assembly. Catch the exception and call System.Runtime.InteropServices.Marshal.GetHRForException() to get its hresult to find out.

Note that this assumes that you are not concerned about performance. If you are concerned about that, then the way to optimize for it is to not try to determine whether files are assemblies at runti

Also see: DevWeek 2008 Cross Platform Silverlight Demos

Also see: Never keep your emotions bottled up

Also see: From C# to Java: Part 4

Also see: ReflectionTypeLoadException

Also see: AppDomains (”application domains”)

Also see: Help John Baez and Mike Stay!

Also see: Java design, operator overloading and people

Also see: A Couple of My Rules for Startups

Also see: An Interview with Robin Milner

Also see: Link Love: 09/21/2007

Also see: ASP.NET MVC in CodePlex and Extensible Unit Testing

Also see: Startup, Shutdown and related matters

Also see: Link Love: 09/21/2007

Also see: Silverlight 2 Beta 1 Cross Domain Bug

Also see: Big in Japan

Also see: Memory Model

Also see: C# 3.0 Lambdas and Type Inference

Also see: Link Love: 09/21/2007

Also see: Java Frameworks State of the (dis)Union.

Also see: Eriskay: a Programming Language Based on Game Semantics

Also see: SIGPLAN Workshop on Undergraduate Programming Language Curriculum

Also see: Note to self: Blog about using Service Broker

Also see: DevWeek 2008 Cross Platform Silverlight Demos

Also see: From C# to Java: Part 3

Also see: C# 3.0 Lambdas and Type Inference

Also see: ReflectionTypeLoadException

Also see: Silverlight and WPF Control Developer Huddle at Mix08

Also see: There can be only one… with data

Also see: Merry Christmas Indeed!

Also see: Resizing a Form has always been a pain in the rectum…

Also see: Brad Abrams’ pixel8 Interview Podcast posted

Also see: Passing the Community Torch: In Search of a New Chief Executive in Redmond

Also see: Fix ReturnUrl When Sharing Forms Authentication with Multiple Web Applications

Also see: Fix ReturnUrl When Sharing Forms Authentication with Multiple Web Applications

Also see: The 2 Technology Magazines You Should Read

Also see: Passing the Community Torch: In Search of a New Chief Executive in Redmond

Also see: On the Perils of Wikipedia

Also see: Silverlight 2 Beta 1 Cross Domain Bug

Also see: Passing the Community Torch: In Search of a New Chief Executive in Redmond

Also see: C# 3.0 Lambdas and Type Inference

Also see: ReflectionTypeLoadException

Also see: A quick update on me.

Also see: Brad Abrams’ pixel8 Interview Podcast posted

Also see: LINQ - The Uber FindControl

Also see: Single source code base for Silverlight and WPF solutions

Also see: A Quick Fix for the Validator SetFocusOnError Bug

Also see: When Are Two Algorithms the Same?

Also see: Big in Japan

Also see: The Exception Model

Also see: Silverlight 2 Beta 1 Cross Domain Bug

me at all
. (Some people are concerned about the exception that is thrown if the file is not an assembly. They overlook that in order to determine that it is not one, it has to be loaded and then thrown away, which can be even more expensive than catching an exception!) Instead, require that only assemblies are given to you. Then, if a file is not an assembly, it will be an exceptional situation. That way, you avoid the bad perf of both the exception and the unnecessary file loading.


http://blogs.msdn.com/suzcook/archive/2004/03/17/determining-whether-a-file-is-an-assembly.aspx

Comments »

The URI to TrackBack this entry is: http://thelivechatsoftware.blogsome.com/2008/03/25/determining-whether-a-file-is-an-assembly-2/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com