-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtils.cs
50 lines (42 loc) · 1.7 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DrakiaXYZ.BigBrain
{
internal class Utils
{
public static FieldInfo GetFieldByType(Type classType, Type fieldType)
{
return AccessTools.GetDeclaredFields(classType).FirstOrDefault(
x => fieldType.IsAssignableFrom(x.FieldType) || (x.FieldType.IsGenericType && fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().IsAssignableFrom(x.FieldType.GetGenericTypeDefinition())));
}
public static string GetPropertyNameByType(Type classType, Type propertyType)
{
return AccessTools.GetDeclaredProperties(classType).FirstOrDefault(
x => propertyType.IsAssignableFrom(x.PropertyType) || (x.PropertyType.IsGenericType && propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(x.PropertyType.GetGenericTypeDefinition())))?.Name;
}
public static bool HasSameContents<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
{
if (collection1.Count() != collection2.Count())
{
return false;
}
if (collection1.Any(item => !collection2.Contains(item)))
{
return false;
}
return true;
}
internal static string CreateCollectionText<T>(IEnumerable<T> items, int maxItemsToList = 10)
{
int itemCount = items.Count();
if (itemCount > maxItemsToList)
{
return $"{itemCount} {typeof(T).Name}s";
}
return string.Join(", ", items);
}
}
}