lundi 3 mai 2021

How Can I Get Target Framework Of An Assembly?

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": ""
  }




Aucun commentaire:

Enregistrer un commentaire