mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Custom Serializers
- Dictionary serializer
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user