Skip to content

Commit

Permalink
UPDATED: Various Files
Browse files Browse the repository at this point in the history
- Updated LoggerWebApplicationBuilderExtensions.cs: Modified the configureApplicationInsightsLoggerOptions method.
- Updated Dgmjr.System.Extensions.csproj: Added a package reference to system.ComponentModel.Annotations.
- Updated EnumExtensions.cs: Added new methods for getting field info, custom attribute, short name, display name, and order.
- Added Enums.cs: Added a new file with methods for getting enum values and parsing enum strings.
- Added ObjectExtensions.cs: Added new extension methods for working with assembly resources and getting property values.
  • Loading branch information
dgmjr committed Feb 27, 2024
1 parent 50244ff commit 893e806
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Logging/LoggerWebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ this WebApplicationBuilder builder
config.ConnectionString = builder.Configuration.GetConnectionString(
ApplicationInsights
),
configureApplicationInsightsLoggerOptions: _ => { }
configureApplicationInsightsLoggerOptions: _ => { }
)
#if DEBUG
.AddConsole()
Expand Down
1 change: 1 addition & 0 deletions System/Dgmjr.System.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<PackageReference Include="System.Usings" IncludeAssets="Build" ExcludeAssets="Compile;Runtime;Native;Analyzers" />
<PackageReference Include="System.Text.Usings" IncludeAssets="Build" ExcludeAssets="Compile;Runtime;Native;Analyzers"/>
<PackageReference Include="system.Xml.ReaderWriter" Condition="$(TargetFramework.StartsWith('net6')) Or $(TargetFramework.StartsWith('net7'))"/>
<PackageReference Include="system.ComponentModel.Annotations" Condition="$(TargetFramework.StartsWith('netstandard'))"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Memory" />
Expand Down
33 changes: 30 additions & 3 deletions System/System/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace System;

using System.ComponentModel.DataAnnotations;
public static class EnumExtensions
{
public static int ToInt32<T>(this T value)
Expand All @@ -9,8 +9,8 @@ public static int ToInt32<T>(this T value)
: throw new InvalidCastException("Could not convert to int32");

public static long ToInt64<T>(this T value)
where T : Enum
=> value.ConvertTo<T, long>(out var result)
where T : Enum =>
value.ConvertTo<T, long>(out var result)
? result
: throw new InvalidCastException("Could not convert to int64");

Expand All @@ -37,4 +37,31 @@ public static bool ConvertTo<T, U>(this T value, out U result)
return false;
}
}

public static FieldInfo GetFieldInfo<T>(this T e)
where T : Enum => e.GetType().GetField(e.ToString());

public static TAttribute GetCustomAttribute<TAttribute>(this Enum e)
where TAttribute : Attribute => e.GetFieldInfo().GetCustomAttribute<TAttribute>();

public static string GetShortName<T>(this T e)
where T : Enum
{
var attribute = e.GetCustomAttribute<DisplayAttribute>();
return attribute?.ShortName?.IsPresent() == true ? attribute.GetShortName() : e.ToString();
}

public static string GetDisplayName<T>(this T e)
where T : Enum
{
var attribute = e.GetCustomAttribute<DisplayAttribute>();
return attribute?.ShortName?.IsPresent() == true ? attribute.GetName() : e.ToString();
}

public static int GetOrder<T>(this T e)
where T : Enum
{
var attribute = e.GetCustomAttribute< DisplayAttribute>();
return attribute?.GetOrder() ?? 0;
}
}
15 changes: 15 additions & 0 deletions System/System/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace System;

public static class Enums
{
public static T[] GetValues<T>()
where T : Enum
{
return Enum.GetValues(typeof(T)).OfType<T>().ToArray();
}
public static T Parse<T>(string s)
where T : Enum
{
return Enum.Parse(typeof(T), s, true).To<T>();
}
}
27 changes: 27 additions & 0 deletions System/System/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace System;

public static class ObjectExtensions
{
public static Stream GetManifestResourceStream<T>(this T _, string resourceName)
=> typeof(T).Assembly.GetManifestResourceStream(resourceName);
public static byte[] GetManifestResourceBytes<T>(this T _, string resourceName)
=> typeof(T).Assembly.GetManifestResourceStream(resourceName).ReadAllBytes();
public static async Task<byte[]> GetManifestResourceBytesAsync<T>(this T _, string resourceName)
=> await typeof(T).Assembly.GetManifestResourceStream(resourceName).ReadAllBytesAsync();
public static string ReadAssemblyResourceAllText<T>(this T _, string resourceName)
=> Extensions.ReadAssemblyResourceAllText(typeof(T).Assembly, resourceName);
public static async Task<string> ReadAssemblyResourceAllTextAsync<T>(this T _, string resourceName)
=> await Extensions.ReadAssemblyResourceAllTextAsync(typeof(T).Assembly, resourceName);

public static T To<T>(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<T>(this object obj, string propertyName)
{
return (T?)obj.GetType().GetRuntimeProperties().FirstOrDefault(pi => pi.Name.Equals(propertyName, OrdinalIgnoreCase))?.GetValue(obj);
}
}

0 comments on commit 893e806

Please sign in to comment.