Multi-Platform Package Manager for Stable Diffusion
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.
 
 
 

26 lines
798 B

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));
}
}