How determine that assembly was compiled from f# project?

Viewed 97

What is the difference between c# and f# assemblies? Some flag maybe? I want to determine it using reflection API only

1 Answers

There's no single value to check that would tell you what you need, but there's a good amount of circumstancial evidence that you could look at - IlSpy is your friend if you want to explore it.

I would suggest you check for presence of these two indicators, either of them being present would mean you're likely looking at an F# assembly unless someone is really dedicated to mess things up for you.

  • FSharpInterfaceDataVersionAttribute on the assembly. This was my initial suggestion, however there are compiler flags that, when set, would prevent this attribute from being emitted: --standalone and --nointerfacedata. I find it highly doubtful either of them would be commonly used in the field, but the fact remains there are openly available ways of opting out from the attribute being emitted right now.

    asm.GetCustomAttribute(typeof(FSharpInterfaceDataVersionAttribute))
    
  • Presence of StartupCode types. They're an artifact of how F# compiler compiles certain constructs, and it seems they're present even empty, so they should be highly reliable.

    asm.GetTypes().Where(fun x -> x.FullName.StartsWith("<StartupCode$"))
    

In particular looking for a reference to FSharp.Core is not a great idea, as it would be commonly referenced from C# projects as well if you're working with mixed solutions (and there's nothing stopping anyone from just getting it off nuget).

Related