canto/Assets/Scripts/Extension/Collection.cs
2025-08-06 12:51:11 -04:00

47 lines
1.2 KiB
C#

using System;
namespace KitsuneCafe.Extension
{
public static class CollectionExtension
{
public static bool IsTypeOf<T, U>()
{
return IsTypeOf(typeof(T), typeof(U));
}
public static bool IsTypeOf<T>(this object value)
{
return IsTypeOf(value.GetType(), typeof(T));
}
public static bool IsTypeOf(this object value, Type type)
{
return IsTypeOf(value.GetType(), type);
}
public static bool IsTypeOf<T>(this Type type)
{
if (type == null) { return false; }
return IsTypeOf(type, typeof(T));
}
public static bool IsTypeOf(this Type type, Type other)
{
if (type == null || other == null || !other.IsGenericTypeDefinition)
{
return false;
}
return other.IsGenericType && other.GetGenericTypeDefinition() == type;
}
public static Type GetGenericArgument(this object value)
{
if (value == null) { return null; }
var type = value.GetType();
var args = type.GetGenericArguments();
return args.Length > 0 ? args[0] : null;
}
}
}