Polyglot is a small library that will attempt to determine the original source language of a compiled CLI (.NET) assembly.
Polyglot supports these platforms:
- .NET Framework 4.5.1 and higher
- CoreCLR/DNX (or any
dotnet5.1
/netstandard1.0
platform)
Polyglot includes heuristics for code written in these languages:
- C#
- VB.NET (only on desktop .NET, currently!)
- F#
Polyglot will not always work, so don't rely on this for anything mission-critical. Incorrect results could occur if:
- There isn't enough data to make an educated guess!
- Language-specific libraries are reused (
Microsoft.VisualBasic
in a C# project, for instance) - Internal compiler semantics change (hello, Roslyn)
- Someone deliberately wants to fool the analyzer
- You think merging assemblies is a fun thing to do
// Analyze the current assembly.
var assembly = this.GetType().GetTypeInfo().Assembly;
// Analyze the calling assembly. Cool!
// (Only works on desktop .NET)
var assembly = Assembly.GetCallingAssembly();
// Analyze an assembly from disk.
var assembly = Assembly.LoadFrom(path);
// Analyze!
var analyzer = new AssemblyAnalyzer(assembly);
// analyzer.DetectedLanguage is the best guess,
// analyzer.AllResults shows all matching heuristics.
This library is based on a StackOverflow answer I wrote that attempted to do the same thing. I was surprised that nothing else existed, so I took some hints from other questions and built a proof-of-concept.
It's solidly in the realm of "trickery that doesn't always work" (see caveats, above). But, works well enough for me to use for some rudimentary diagnostic reporting in the Stormpath.SDK library, which I maintain.