60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace KitsuneCafe.Extension
|
|
{
|
|
public static class CollectionExtension
|
|
{
|
|
public static bool HasIndex(this IList list, int index)
|
|
{
|
|
return index >= 0 && index < list.Count;
|
|
}
|
|
|
|
public static IEnumerable<(T, int)> Enumerate<T>(this IEnumerable<T> enumerable)
|
|
{
|
|
return enumerable.Select((x, i) => (x, i));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|