Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • C# - Has AssemblyConfigurationAttribute, Has GuidAttribute
  • VB - Has many extra "My" type (ex. MyApplication, MySettings), Has GuidAttibute
  • F# - Has a FSharpInterfaceDataVersionAttribute which also specifies the version of the compiler used.
  • C++ (all but /clr:safe) - Has a bunch of extra types (FrameInfo, type_info)
  • C++ /clr:safe - Seems to have no unique reflection features.

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.

like image 434
Rick Minerich Avatar asked Feb 23 '09 22:02

Rick Minerich


People also ask

What does a .NET assembly contain?

NET Framework, assemblies can contain one or more modules. This allows larger projects to be planned so that several developers can work on separate source code files or modules, which are combined to create a single assembly. For more information about modules, see How to: Build a multifile assembly.

How do I know if a file is .NET assembly?

Call the GetMetadataReader method on the PE reader instance to create a metadata reader. Check the value of the IsAssembly property. If the value is true , the file is an assembly.

Which is an assembly format in NET?

. NET defines a binary file format, assembly, that is used to fully describe and contain . NET programs. Assemblies are used for the programs themselves as well as any dependent libraries.

Where can I find .NET assemblies?

These are all stored in the GAC at %WINDIR%\assembly , which you can view in Windows Explorer. The actual dlls can be found in subfolders of the GAC , GAC_32 , and GAC_MSIL folders, seemingly dependent on their version.


2 Answers

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)

like image 132
Ana Betts Avatar answered Oct 21 '22 08:10

Ana Betts


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.

like image 34
m-sharp Avatar answered Oct 21 '22 08:10

m-sharp