Rick Minerich
Rick Minerich

Reputation: 3088

Is it possible to determine in which language a .NET Assembly was written ex post facto?

This started as a way to find C++/CLI and Managed C++ assemblies so that all classes internal to them could be tested to ensure all inherited methods were being reimplemented. I would like to add this as a build process step to ensure that it never happens again.

Thinking about this problem also made me a bit curious as it would be interesting to be able to determine any .NET language used. Because of this, I went a bit further and compared assemblies from all of the .NET languages. So far here is what I've found through a small program I wrote which compares the type and attribute data from any set of .NET assemblies via reflection:

It might be reasonable to parse in this order:

  1. It's F# if it has the FSharpInterfaceDataVersionAttribute
  2. It's C++ if it has any in the huge set of extra types I found.
  3. It's VB if it has the "My*" Types.
  4. It's C# if it has AssemblyConfigurationAttribute or GuidAttribute
  5. It's likely to be C++ /clr:Safe

However, as this is a horrible hack, I wanted to check in here to make sure that there wasn't another option available.

Upvotes: 10

Views: 570

Answers (2)

Ana Betts
Ana Betts

Reputation: 74682

Checking the references for things like the VB or F# class libraries seems to be the least shaky way to do this, but as others mention, it's a heuristic - just like there's no definitive way to tell which language a native binary is written in (but you can be almost 100% sure by heuristics)

Upvotes: 3

m-sharp
m-sharp

Reputation: 17135

When a .NET language is compiled, all you get is IL. I am not aware of a standard way of determining which specific language created the assembly. You can take an existing assembly and ildasm (disassemble) it into IL and them ilasm (assemble) it back into a virtually identical assembly.

The heuristics you use is a reasonable and clever way to identify the language used to create the assembly. However, bear in mind that these details might change between compiler versions of the languages.

Upvotes: 2

Related Questions