mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-19 06:53:50 +02:00
40 lines
934 B
C#
40 lines
934 B
C#
using System;
|
|
using System.Text.Json;
|
|
|
|
namespace ModernWpfPlayground.MvvmStuff
|
|
{
|
|
public static class PropertyInfoExtension
|
|
{
|
|
public static object? Convert(this JsonElement value, Type propertyType)
|
|
{
|
|
if (propertyType == null) return default;
|
|
|
|
if (propertyType == typeof(double))
|
|
{
|
|
return value.GetDouble();
|
|
}
|
|
|
|
if (propertyType == typeof(bool))
|
|
{
|
|
return value.GetBoolean();
|
|
}
|
|
|
|
if (propertyType == typeof(int))
|
|
{
|
|
return value.GetInt32();
|
|
}
|
|
|
|
if (propertyType.IsEnum)
|
|
{
|
|
return Enum.ToObject(propertyType, value.GetInt32());
|
|
}
|
|
|
|
if (propertyType == typeof(string))
|
|
{
|
|
return value.GetString();
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|
|
} |