Table of Contents

Class TargetFrameworkMoniker

Namespace
Cuemon.Reflection
Assembly
Cuemon.Core.dll

Provides a set of static methods for resolving target framework monikers from assemblies and the current application context.

public static class TargetFrameworkMoniker
Inheritance
TargetFrameworkMoniker

Examples

TargetFrameworkMoniker parses and resolves short target framework names such as net10.0, net9.0, netstandard2.0, and net481 from framework names, assemblies, paths, or the current application context.

using System;
using System.Reflection;
using Cuemon.Reflection;

namespace MyApp.Examples;

public class TargetFrameworkMonikerExample
{
    public void Demonstrate()
    {
        var parsed = TargetFrameworkMoniker.Parse(".NETCoreApp,Version=v10.0");
        var current = TargetFrameworkMoniker.ResolveCurrent();
        var library = TargetFrameworkMoniker.Resolve(typeof(TargetFrameworkMonikerExample).Assembly);
        var outputPath = TargetFrameworkMoniker.ResolveFromPath(AppContext.BaseDirectory);

        Console.WriteLine($"Parsed TFM: {parsed}");
        Console.WriteLine($"Current TFM: {current}");
        Console.WriteLine($"Example assembly TFM: {library}");
        Console.WriteLine($"Output path TFM: {outputPath}");

        if (TargetFrameworkMoniker.TryResolve(Assembly.GetExecutingAssembly(), out var executingAssemblyTfm))
        {
            Console.WriteLine($"Executing assembly TFM: {executingAssemblyTfm}");
        }
    }
}

Methods

Parse(FrameworkName)

Parses the specified frameworkName into its short target framework moniker representation.

public static string Parse(FrameworkName frameworkName)

Parameters

frameworkName FrameworkName

The FrameworkName to parse.

Returns

string

The parsed target framework moniker, or null if frameworkName could not be parsed as a supported target framework moniker.

Parse(string)

Parses the specified frameworkNameOrTargetFrameworkMoniker into its short target framework moniker representation.

public static string Parse(string frameworkNameOrTargetFrameworkMoniker)

Parameters

frameworkNameOrTargetFrameworkMoniker string

The framework name or target framework moniker to parse.

Returns

string

The parsed target framework moniker, or null if frameworkNameOrTargetFrameworkMoniker could not be parsed as a supported target framework moniker.

Resolve(Assembly)

Resolves the target framework moniker of the specified assembly.

public static string Resolve(Assembly assembly)

Parameters

assembly Assembly

The Assembly to inspect for a TargetFrameworkAttribute.

Returns

string

The resolved target framework moniker of the specified assembly, or null if no supported moniker could be resolved.

Exceptions

ArgumentNullException

assembly is null.

ResolveCurrent()

Resolves the target framework moniker of the current application context.

public static string ResolveCurrent()

Returns

string

The resolved target framework moniker of the current application context, or null if no supported moniker could be resolved.

Remarks

Resolution first inspects the entry assembly for a TargetFrameworkAttribute. If no supported framework name is found, the directory hierarchy rooted at BaseDirectory is inspected for a target-framework-like folder name.

ResolveFromPath(string)

Resolves the nearest target framework moniker found in the specified path.

public static string ResolveFromPath(string path)

Parameters

path string

The path whose directory hierarchy should be inspected for a target-framework-like folder name.

Returns

string

The nearest resolved target framework moniker found in the specified path, or null if no supported moniker could be resolved.

TryParse(FrameworkName, out string)

Attempts to parse the specified frameworkName into its short target framework moniker representation.

public static bool TryParse(FrameworkName frameworkName, out string targetFrameworkMoniker)

Parameters

frameworkName FrameworkName

The FrameworkName to parse.

targetFrameworkMoniker string

When this method returns, contains the parsed target framework moniker, or null if frameworkName could not be parsed as a supported target framework moniker.

Returns

bool

true if frameworkName was parsed successfully; otherwise, false.

TryParse(string, out string)

Attempts to parse the specified frameworkNameOrTargetFrameworkMoniker into its short target framework moniker representation.

public static bool TryParse(string frameworkNameOrTargetFrameworkMoniker, out string targetFrameworkMoniker)

Parameters

frameworkNameOrTargetFrameworkMoniker string

The framework name or target framework moniker to parse.

targetFrameworkMoniker string

When this method returns, contains the parsed target framework moniker, or null if frameworkNameOrTargetFrameworkMoniker could not be parsed as a supported target framework moniker.

Returns

bool

true if frameworkNameOrTargetFrameworkMoniker was parsed successfully; otherwise, false.

TryResolve(Assembly, out string)

Attempts to resolve the target framework moniker of the specified assembly.

public static bool TryResolve(Assembly assembly, out string targetFrameworkMoniker)

Parameters

assembly Assembly

The Assembly to inspect for a TargetFrameworkAttribute.

targetFrameworkMoniker string

When this method returns, contains the resolved target framework moniker of the specified assembly, or null if the operation failed.

Returns

bool

true if the target framework moniker of the specified assembly was resolved successfully; otherwise, false.

Exceptions

ArgumentNullException

assembly is null.

TryResolveCurrent(out string)

Attempts to resolve the target framework moniker of the current application context.

public static bool TryResolveCurrent(out string targetFrameworkMoniker)

Parameters

targetFrameworkMoniker string

When this method returns, contains the resolved target framework moniker of the current application context, or null if the operation failed.

Returns

bool

true if the target framework moniker of the current application context was resolved successfully; otherwise, false.

Remarks

Resolution first inspects the entry assembly for a TargetFrameworkAttribute. If no supported framework name is found, the directory hierarchy rooted at BaseDirectory is inspected for a target-framework-like folder name.

TryResolveFromPath(string, out string)

Attempts to resolve the nearest target framework moniker found in the specified path.

public static bool TryResolveFromPath(string path, out string targetFrameworkMoniker)

Parameters

path string

The path whose directory hierarchy should be inspected for a target-framework-like folder name.

targetFrameworkMoniker string

When this method returns, contains the nearest resolved target framework moniker found in the specified path, or null if the operation failed.

Returns

bool

true if a target framework moniker was resolved successfully from the specified path; otherwise, false.