0

I am loading different assemblies using Mono.Cecil and I would like to know if the assembly is targeting .NET Framework or .NET Core.

What I have tried is retrieving the custom attribute TargetFrameworkAttribute :

[assembly:TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework)]

using the code below

    var targetFrameWorkAttributes = assemblyDefinition.CustomAttributes
        .Where(attribute => attribute.AttributeType.Name == nameof(TargetFrameworkAttribute));
    var customAttribute = targetFrameWorkAttributes.FirstOrDefault();
    var customAttributeValue =  customAttribute?.ConstructorArguments.FirstOrDefault().Value.ToString();

The problem here is that not all assemblies have this attribute in their metadata.

My question is :

Is there any other way to detect if an assembly is targeting .NET Core or .NET Framework runtime other than retrieving custom attribute?

N.B. I have thought for .NET Core I could somehow parse *.deps.json and retrieve runtimeTarget object, but it's only for .NET Core and it would be complicated.

  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v3.1",
    "signature": ""
  }
  • Does this answer your question? [Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly](https://stackoverflow.com/questions/6854664/retrieve-target-framework-version-and-target-framework-profile-from-a-net-assem) – Palle Due May 03 '21 at 10:49
  • @PalleDue No, all responses there are talking about Custom Attribute, but I mention that it's not the solution for my case because there are assemblies that don't Contain this information – youssef jirari May 03 '21 at 10:59
  • A possible fallback is to look for the dependent assembly that defines BCL types like System.String. Iterate cecil's equivalent of Assembly.GetReferencedAssemblies() and look for either "netstandard", "mscorlib" or "System.Private.CoreLib". The latter may change, not so likely. – Hans Passant May 03 '21 at 11:18
  • @HansPassant Yeah, i suppose that you are suggesting if for exemple an assembly targets .NET Framework would have "mscorlib" as a dependency, and for .NET Core "System.Runtime" and so on, i think it still a solution but it's not that much reliable. – youssef jirari May 03 '21 at 11:35

0 Answers0