Custom Serializers

- Dictionary serializer
This commit is contained in:
Simon Lübeß
2024-01-05 13:37:58 +01:00
parent 16f4dd349b
commit 3de861110e
8 changed files with 376 additions and 60 deletions
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace GlitchyEngine.Extensions;
public static class KeyValuePairExtension
{
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
value = tuple.Value;
}
}
+50
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace GlitchyEngine.Extensions;
@@ -15,4 +16,53 @@ public static class TypeExtension
{
return targetType.IsAssignableFrom(type);
}
/// <summary>
/// Enumerates all types in all assemblies.
/// </summary>
public static IEnumerable<Type> EnumerateAllTypes()
{
foreach (Assembly domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type type in domainAssembly.GetTypes())
{
yield return type;
}
}
/// <summary>
/// Enumerates all types that derive from the given type.
/// </summary>
/// <param name="baseType"></param>
/// <returns></returns>
public static IEnumerable<Type> FindDerivedTypes(Type baseType)
{
foreach (Assembly domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type type in domainAssembly.GetTypes())
{
if (baseType.IsAssignableFrom(type) && !type.IsAbstract) yield return type;
}
}
/// <summary>
/// Returns whether or not the type has an <see cref="Attribute"/> of the specified <see cref="Type"/> <see cref="T"/>.
/// </summary>
/// <typeparam name="T">The type of the attribute</typeparam>
/// <returns><see langword="true"/> if the type has the specified <see cref="Attribute"/>; <see langword="true"/> otherwise.</returns>
public static bool HasCustomAttribute<T>(this Type type) where T: Attribute
{
return type.GetCustomAttribute<T>() != null;
}
/// <summary>
/// Returns whether or not the type has an <see cref="Attribute"/> of the specified <see cref="Type"/> <see cref="T"/>.
/// </summary>
/// <typeparam name="T">The type of the attribute</typeparam>
/// <param name="attribute">The attribute, or <seealso langword="null"/>, if the type hasn't got the attribute specified.</param>
/// <returns><see langword="true"/> if the type has the specified <see cref="Attribute"/>; <see langword="true"/> otherwise.</returns>
public static bool TryGetCustomAttribute<T>(this Type type, out T attribute) where T: Attribute
{
attribute = type.GetCustomAttribute<T>();
return attribute != null;
}
}