ProjectDDD/Assets/_Datas/SLShared/SLSystem/SLSystemUtility.cs
2025-06-17 20:47:57 +09:00

197 lines
7.3 KiB (Stored with Git LFS)
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Superlazy
{
public static class SLSystemUtility
{
public static void CreateTypeList<T>(this List<Type> ret) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
ret.Add(t);
});
}
public static void CreateInstanceList<T>(this List<T> ret) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
var obj = a.CreateInstance(t.FullName);
ret.Add(obj as T);
});
}
public static void CreateInstanceList<T>(this List<T> ret, string[] typeNames) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
if (typeNames.FirstOrDefault(n => n == t.FullName) == null) return;
var obj = a.CreateInstance(t.FullName);
ret.Add(obj as T);
});
}
public static void CreateInstanceDictionary<T>(this Dictionary<string, T> ret) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
var obj = a.CreateInstance(t.FullName);
ret.Add(t.Name, obj as T);
});
}
public static void CreateInstanceDictionary<T>(this Dictionary<string, T> ret, string[] typeNames) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
if (typeNames.FirstOrDefault(n => n == t.FullName) == null) return;
var obj = a.CreateInstance(t.FullName);
ret.Add(t.Name, obj as T);
});
}
public static void CreateMethodInfoDictionary<T>(this Dictionary<string, Dictionary<string, MethodInfo>> ret, bool useReturn, params Type[] argTypes) where T : class
{
ForeachTypeof(typeof(T), (a, t) =>
{
var methodArr = t.GetMethods();
var methodDic = new Dictionary<string, MethodInfo>();
for (var i = 0; i < methodArr.Length; i++)
{
var method = methodArr[i];
var parameters = method.GetParameters();
if (parameters.CompareLengthAndTypes(argTypes.Skip(useReturn ? 1 : 0).ToArray()) && (useReturn ? method.ReturnType == argTypes[0] : method.ReturnType == typeof(void)))
methodDic.Add(method.Name, method);
}
ret.Add(t.Name, methodDic);
});
}
public static void CreateCommandInfoToBaseType<T>(this Dictionary<string, (T instance, MethodInfo method)> ret, T instance, params Type[] argTypes) where T : class
{
var methodArr = instance.GetType().GetMethods();
for (var i = 0; i < methodArr.Length; i++)
{
var method = methodArr[i];
var parameters = method.GetParameters();
if (parameters.CompareLengthAndTypes(typeof(SLEntity)))
{
ret.Add(string.Format("{0}/{1}", instance.GetType().Name, method.Name), (instance, method));
}
}
}
public static List<MethodInfo> GetMethods<T>() where T : class
{
var methods = new List<MethodInfo>();
var rootType = typeof(T);
ForeachTypeof(typeof(T), (a, t) =>
{
foreach (var m in t.GetMethods())
{
methods.Add(m);
}
});
return methods;
}
private static bool IsInherited(this Type t, Type inherited)
{
if (t.BaseType == null || t.BaseType == typeof(object))
return false;
if (t.BaseType == inherited)
return true;
return t.BaseType.IsInherited(inherited);
}
private static Type GetRootBaseType(Type t)
{
if (t.BaseType == null || t.BaseType == typeof(object))
return t;
else
return GetRootBaseType(t.BaseType);
}
public static bool CompareLengthAndTypes(this ParameterInfo[] parameters, params Type[] types)
{
if (parameters.Length != types.Length)
return false;
for (var i = 0; i < parameters.Length; i++)
{
if (parameters[i].ParameterType != types[i])
{
return false;
}
}
return true;
}
public static T CreateInstance<T>(string typeName) where T : class
{
var type = AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetTypes().FirstOrDefault(t => t.Name == typeName))
.FirstOrDefault(t => t != null);
if (type == null) return null;
return type.Assembly.CreateInstance(type.FullName) as T;
}
private static void ForeachTypeof(Type rootType, Action<Assembly, Type> action)
{
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
if (a.FullName.IsLeft("Mono.")) continue;
if (a.FullName.IsLeft("SyntaxTree.")) continue;
if (a.FullName.IsLeft("nunit.")) continue;
if (a.FullName.IsLeft("Unity.")) continue;
if (a.FullName.IsLeft("ICSharpCode.")) continue;
if (a.FullName.IsLeft("Newtonsoft.")) continue;
if (a.FullName.IsLeft("Facebook")) continue;
if (a.FullName.IsLeft("winrt")) continue;
if (a.FullName.IsLeft("Tizen")) continue;
if (a.FullName.IsLeft("Apple")) continue;
if (a.FullName.IsLeft("Security")) continue;
if (a.FullName.IsLeft("Stores")) continue;
if (a.FullName.IsLeft("Purchasing.")) continue;
if (a.FullName.IsLeft("UnityStore")) continue;
if (a.FullName.IsLeft("Editor")) continue;
if (a.FullName.IsLeft("ChannelPurchase")) continue;
if (a.FullName.IsLeft("Google.")) continue;
if (a.FullName.IsLeft("UnityScript")) continue;
if (a.FullName.IsLeft("Boo.Lan")) continue;
if (a.FullName.IsLeft("System")) continue;
if (a.FullName.IsLeft("I18N")) continue;
if (a.FullName.IsLeft("UnityEngine")) continue;
if (a.FullName.IsLeft("UnityEditor")) continue;
if (a.FullName.IsLeft("mscorlib")) continue;
if (a.FullName.IsLeft("NiceIO")) continue;
if (a.FullName.IsLeft("PP")) continue;
if (a.FullName.IsLeft("PlayerBuild")) continue;
if (a.FullName.IsLeft("AndroidPlayerBuild")) continue;
if (a.FullName.IsLeft("Ex")) continue;
if (a.FullName.IsLeft("ScriptCompilation")) continue;
if (a.FullName.IsLeft("Anonymously")) continue;
foreach (var t in a.GetTypes())
{
if (t.IsAbstract) continue;
if (t.IsClass == false) continue;
if (rootType.IsAssignableFrom(t) == false) continue;
action(a, t);
}
}
}
}
}