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
frameworkNameFrameworkNameThe FrameworkName to parse.
Returns
- string
The parsed target framework moniker, or null if
frameworkNamecould 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
frameworkNameOrTargetFrameworkMonikerstringThe framework name or target framework moniker to parse.
Returns
- string
The parsed target framework moniker, or null if
frameworkNameOrTargetFrameworkMonikercould 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
assemblyAssemblyThe 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
assemblyis 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
pathstringThe 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
frameworkNameFrameworkNameThe FrameworkName to parse.
targetFrameworkMonikerstringWhen this method returns, contains the parsed target framework moniker, or null if
frameworkNamecould not be parsed as a supported target framework moniker.
Returns
- bool
trueifframeworkNamewas 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
frameworkNameOrTargetFrameworkMonikerstringThe framework name or target framework moniker to parse.
targetFrameworkMonikerstringWhen this method returns, contains the parsed target framework moniker, or null if
frameworkNameOrTargetFrameworkMonikercould not be parsed as a supported target framework moniker.
Returns
- bool
trueifframeworkNameOrTargetFrameworkMonikerwas 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
assemblyAssemblyThe Assembly to inspect for a TargetFrameworkAttribute.
targetFrameworkMonikerstringWhen this method returns, contains the resolved target framework moniker of the specified
assembly, or null if the operation failed.
Returns
- bool
trueif the target framework moniker of the specifiedassemblywas resolved successfully; otherwise,false.
Exceptions
- ArgumentNullException
assemblyis null.
TryResolveCurrent(out string)
Attempts to resolve the target framework moniker of the current application context.
public static bool TryResolveCurrent(out string targetFrameworkMoniker)
Parameters
targetFrameworkMonikerstringWhen this method returns, contains the resolved target framework moniker of the current application context, or null if the operation failed.
Returns
- bool
trueif 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
pathstringThe path whose directory hierarchy should be inspected for a target-framework-like folder name.
targetFrameworkMonikerstringWhen this method returns, contains the nearest resolved target framework moniker found in the specified
path, or null if the operation failed.
Returns
- bool
trueif a target framework moniker was resolved successfully from the specifiedpath; otherwise,false.