You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
798 B
27 lines
798 B
1 year ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Collections.Immutable;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
using System.Text.Json.Serialization;
|
||
|
|
||
|
namespace StabilityMatrix.Avalonia.Helpers;
|
||
|
|
||
|
public static class ViewModelSerializer
|
||
|
{
|
||
|
public static IImmutableDictionary<string, Type> GetDerivedTypes(Type baseType)
|
||
|
{
|
||
|
return GetJsonDerivedTypeAttributes(baseType)
|
||
|
.ToImmutableDictionary(x => x.typeDiscriminator, x => x.subType);
|
||
|
}
|
||
|
|
||
|
public static IEnumerable<(
|
||
|
Type subType,
|
||
|
string typeDiscriminator
|
||
|
)> GetJsonDerivedTypeAttributes(Type type)
|
||
|
{
|
||
|
return type.GetCustomAttributes<JsonDerivedTypeAttribute>()
|
||
|
.Select(x => (x.DerivedType, x.TypeDiscriminator as string ?? x.DerivedType.Name));
|
||
|
}
|
||
|
}
|