feat: Hinzufügen von ObjectExtensions mit Eigenschaft-zu-Wörterbuch und sicherer Zeichenfolgenkonvertierung
This commit is contained in:
parent
682fb772f7
commit
94c6813306
20
src/Leanetec.EConnect.Infrastructure/ObjectExtensions.cs
Normal file
20
src/Leanetec.EConnect.Infrastructure/ObjectExtensions.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Leanetec.EConnect.Infrastructure;
|
||||||
|
|
||||||
|
public static class ObjectExtensions
|
||||||
|
{
|
||||||
|
public static Dictionary<string, string?> ToPropertyDictionary(this object obj)
|
||||||
|
{
|
||||||
|
return obj
|
||||||
|
.GetType()
|
||||||
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||||
|
.ToDictionary(
|
||||||
|
prop => prop.Name,
|
||||||
|
prop => prop.GetValue(obj).ToSafeString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string? ToSafeString(this object? obj)
|
||||||
|
=> obj is bool b ? (b ? "true" : "false") : obj?.ToString();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user