diff --git a/Logging/LoggerWebApplicationBuilderExtensions.cs b/Logging/LoggerWebApplicationBuilderExtensions.cs index 454bd7b..99a682f 100644 --- a/Logging/LoggerWebApplicationBuilderExtensions.cs +++ b/Logging/LoggerWebApplicationBuilderExtensions.cs @@ -50,7 +50,7 @@ this WebApplicationBuilder builder config.ConnectionString = builder.Configuration.GetConnectionString( ApplicationInsights ), - configureApplicationInsightsLoggerOptions: _ => { } + configureApplicationInsightsLoggerOptions: _ => { } ) #if DEBUG .AddConsole() diff --git a/System/Dgmjr.System.Extensions.csproj b/System/Dgmjr.System.Extensions.csproj index 45df060..9a616dc 100644 --- a/System/Dgmjr.System.Extensions.csproj +++ b/System/Dgmjr.System.Extensions.csproj @@ -43,6 +43,7 @@ + diff --git a/System/System/EnumExtensions.cs b/System/System/EnumExtensions.cs index 576820c..d2d2e45 100644 --- a/System/System/EnumExtensions.cs +++ b/System/System/EnumExtensions.cs @@ -1,5 +1,5 @@ namespace System; - +using System.ComponentModel.DataAnnotations; public static class EnumExtensions { public static int ToInt32(this T value) @@ -9,8 +9,8 @@ public static int ToInt32(this T value) : throw new InvalidCastException("Could not convert to int32"); public static long ToInt64(this T value) - where T : Enum - => value.ConvertTo(out var result) + where T : Enum => + value.ConvertTo(out var result) ? result : throw new InvalidCastException("Could not convert to int64"); @@ -37,4 +37,31 @@ public static bool ConvertTo(this T value, out U result) return false; } } + + public static FieldInfo GetFieldInfo(this T e) + where T : Enum => e.GetType().GetField(e.ToString()); + + public static TAttribute GetCustomAttribute(this Enum e) + where TAttribute : Attribute => e.GetFieldInfo().GetCustomAttribute(); + + public static string GetShortName(this T e) + where T : Enum + { + var attribute = e.GetCustomAttribute(); + return attribute?.ShortName?.IsPresent() == true ? attribute.GetShortName() : e.ToString(); + } + + public static string GetDisplayName(this T e) + where T : Enum + { + var attribute = e.GetCustomAttribute(); + return attribute?.ShortName?.IsPresent() == true ? attribute.GetName() : e.ToString(); + } + + public static int GetOrder(this T e) + where T : Enum + { + var attribute = e.GetCustomAttribute< DisplayAttribute>(); + return attribute?.GetOrder() ?? 0; + } } diff --git a/System/System/Enums.cs b/System/System/Enums.cs new file mode 100644 index 0000000..8f56cc3 --- /dev/null +++ b/System/System/Enums.cs @@ -0,0 +1,15 @@ +namespace System; + +public static class Enums +{ + public static T[] GetValues() + where T : Enum + { + return Enum.GetValues(typeof(T)).OfType().ToArray(); + } + public static T Parse(string s) + where T : Enum + { + return Enum.Parse(typeof(T), s, true).To(); + } +} diff --git a/System/System/ObjectExtensions.cs b/System/System/ObjectExtensions.cs new file mode 100644 index 0000000..5bdb829 --- /dev/null +++ b/System/System/ObjectExtensions.cs @@ -0,0 +1,27 @@ +namespace System; + +public static class ObjectExtensions +{ + public static Stream GetManifestResourceStream(this T _, string resourceName) + => typeof(T).Assembly.GetManifestResourceStream(resourceName); + public static byte[] GetManifestResourceBytes(this T _, string resourceName) + => typeof(T).Assembly.GetManifestResourceStream(resourceName).ReadAllBytes(); + public static async Task GetManifestResourceBytesAsync(this T _, string resourceName) + => await typeof(T).Assembly.GetManifestResourceStream(resourceName).ReadAllBytesAsync(); + public static string ReadAssemblyResourceAllText(this T _, string resourceName) + => Extensions.ReadAssemblyResourceAllText(typeof(T).Assembly, resourceName); + public static async Task ReadAssemblyResourceAllTextAsync(this T _, string resourceName) + => await Extensions.ReadAssemblyResourceAllTextAsync(typeof(T).Assembly, resourceName); + + public static T To(this object value) + => (T)Convert.ChangeType(value, typeof(T)); + + public static object? GetPropertyValue(this object obj, string propertyName) + { + return obj.GetType().GetRuntimeProperties().FirstOrDefault(pi => pi.Name.Equals(propertyName, OrdinalIgnoreCase))?.GetValue(obj); + } + public static T? GetPropertyValue(this object obj, string propertyName) + { + return (T?)obj.GetType().GetRuntimeProperties().FirstOrDefault(pi => pi.Name.Equals(propertyName, OrdinalIgnoreCase))?.GetValue(obj); + } +}