mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-19 06:53:50 +02:00
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
|
|
namespace ModernWpfPlayground.MvvmStuff
|
|
{
|
|
public static class PropertyInfoExtension
|
|
{
|
|
public static PropertyInfo? Find(this PropertyInfo[] infos, string key) => Array.Find(infos, x => x.Name == key);
|
|
|
|
public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value)
|
|
{
|
|
if (propertyInfo == null) return default;
|
|
|
|
if (propertyInfo.PropertyType == typeof(double))
|
|
{
|
|
return value.GetDouble();
|
|
}
|
|
|
|
if (propertyInfo.PropertyType == typeof(bool))
|
|
{
|
|
return value.GetBoolean();
|
|
}
|
|
|
|
if (propertyInfo.PropertyType == typeof(int))
|
|
{
|
|
return value.GetInt32();
|
|
}
|
|
|
|
if (propertyInfo.PropertyType.IsEnum)
|
|
{
|
|
return Enum.ToObject(propertyInfo.PropertyType, value.GetInt32());
|
|
}
|
|
|
|
if (propertyInfo.PropertyType == typeof(string))
|
|
{
|
|
return value.GetString();
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|
|
} |