2024-10-11 10:12:15 +08:00
|
|
|
|
/********************************************************************
|
|
|
|
|
文件: CommonTools.cs
|
|
|
|
|
作者: 梦语
|
|
|
|
|
邮箱: 1982614048@qq.com
|
|
|
|
|
创建时间: 2024/03/29 17:28:19
|
|
|
|
|
最后修改: 梦语
|
|
|
|
|
最后修改时间: 2024/04/03 17:15:36
|
|
|
|
|
功能: 通用工具类
|
|
|
|
|
*********************************************************************/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Ether
|
|
|
|
|
{
|
|
|
|
|
public static class CommonTools
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有子类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parentType">基类类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Type[] GetChildTypes(Type parentType)
|
|
|
|
|
{
|
|
|
|
|
List<Type> listType = new List<Type>();
|
|
|
|
|
|
|
|
|
|
// 获取所有程序集
|
|
|
|
|
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
|
|
|
|
|
|
foreach (Assembly assembly in asms)
|
|
|
|
|
{
|
|
|
|
|
// 遍历程序集下的每一个类型
|
|
|
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
|
|
|
|
|
|
foreach (Type tChild in types)
|
|
|
|
|
{
|
|
|
|
|
if (tChild == parentType || tChild.IsSubclassOf(parentType))
|
|
|
|
|
{
|
|
|
|
|
listType.Add(tChild);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listType.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取类型
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Type GetType(string name)
|
|
|
|
|
{
|
|
|
|
|
// 获取所有程序集
|
|
|
|
|
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
|
|
|
|
|
|
foreach (Assembly assembly in asms)
|
|
|
|
|
{
|
|
|
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
|
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
{
|
|
|
|
|
if (type.Name == name)
|
|
|
|
|
{
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符串转换成枚举
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">枚举类型</typeparam>
|
|
|
|
|
/// <param name="name">转换的字符串名</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T StringToEnum<T>(string name) where T : Enum
|
|
|
|
|
{
|
|
|
|
|
return (T)Enum.Parse(typeof(T), name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取枚举数量
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int GetNumberOfEnumValues<T>() where T : Enum
|
|
|
|
|
{
|
|
|
|
|
return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static).Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<(string, string, string)> GetAllFeildStr(string typeName)
|
|
|
|
|
{
|
|
|
|
|
Type type = GetType(typeName);
|
|
|
|
|
FieldInfo[] allField = type.GetFields(BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
|
|
|
|
|
List<(string, string, string)> tempList = new List<(string, string, string)>();
|
|
|
|
|
|
|
|
|
|
foreach (var info in allField)
|
|
|
|
|
{
|
|
|
|
|
string fieldName = info.Name;
|
|
|
|
|
string fieldType = info.FieldType.Name.ToString();
|
|
|
|
|
string fieldValue = info.GetValue(type).ToString();
|
|
|
|
|
|
|
|
|
|
tempList.Add((fieldName, fieldType, fieldValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tempList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取枚举的所有值名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string[] GetAllEnumName<T>() where T : Enum
|
|
|
|
|
{
|
|
|
|
|
FieldInfo[] allEnum = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
string[] names = new string[allEnum.Length];
|
|
|
|
|
for (int i = 0; i < allEnum.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
names[i] = allEnum[i].Name;
|
|
|
|
|
}
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取枚举属性对应的attribute
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <typeparam name="K"></typeparam>
|
|
|
|
|
/// <param name="enumValue"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static K[] GetEnumFieldAllAttribute<T, K>() where T : Enum where K : Attribute
|
|
|
|
|
{
|
|
|
|
|
//反射获取字段
|
|
|
|
|
FieldInfo[] allFields = typeof(T).GetFields();
|
|
|
|
|
|
|
|
|
|
List<FieldInfo> fields = new List<FieldInfo>();
|
|
|
|
|
|
|
|
|
|
foreach (FieldInfo field in allFields)
|
|
|
|
|
{
|
|
|
|
|
if (field.FieldType == typeof(T))
|
|
|
|
|
{
|
|
|
|
|
fields.Add(field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Type attType = typeof(K);
|
|
|
|
|
K[] allAttributeValue = new K[fields.Count];
|
|
|
|
|
for (int i = 0; i < fields.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (fields[i].IsDefined(attType, false))
|
|
|
|
|
{
|
|
|
|
|
//获取id
|
|
|
|
|
//int value = (int)fields[i].GetValue(null);
|
|
|
|
|
|
|
|
|
|
//获取api,读取字段的自定义Attribute
|
|
|
|
|
object attribute = fields[i].GetCustomAttributes(typeof(K), false)[0];
|
|
|
|
|
|
|
|
|
|
allAttributeValue[i] = attribute as K;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
allAttributeValue[i] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allAttributeValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取枚举属性对应的attribute
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <typeparam name="K"></typeparam>
|
|
|
|
|
/// <param name="enumValue"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static K GetEnumFieldAttribute<T, K>(int enumValue) where T : Enum where K : Attribute
|
2024-10-27 04:03:15 +08:00
|
|
|
|
{
|
|
|
|
|
return GetEnumFieldAttribute<K>(typeof(T), enumValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static K GetEnumFieldAttribute<K>(Type type, int enumValue) where K : Attribute
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
|
|
|
|
//反射获取字段
|
2024-10-27 04:03:15 +08:00
|
|
|
|
System.Reflection.FieldInfo[] fields = type.GetFields();
|
2024-10-11 10:12:15 +08:00
|
|
|
|
System.Type attType = typeof(K);
|
|
|
|
|
for (int i = 0; i < fields.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (fields[i].IsDefined(attType, false))
|
|
|
|
|
{
|
|
|
|
|
//获取id
|
|
|
|
|
int value = (int)fields[i].GetValue(null);
|
|
|
|
|
|
|
|
|
|
if (value == enumValue)
|
|
|
|
|
{
|
|
|
|
|
//获取api,读取字段的自定义Attribute
|
|
|
|
|
object attribute = fields[i].GetCustomAttributes(typeof(K), false)[0];
|
|
|
|
|
|
|
|
|
|
return attribute as K;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// 获取对应枚举的描述文字
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <typeparam name="T"></typeparam>
|
|
|
|
|
///// <typeparam name="K"></typeparam>
|
|
|
|
|
///// <param name="enumValue"></param>
|
|
|
|
|
///// <returns></returns>
|
|
|
|
|
//public static string GetEnumLabelText<T, K>(int enumValue) where T : Enum where K : Attribute
|
|
|
|
|
//{
|
|
|
|
|
// LabelTextAttribute attribute = GetEnumFieldAttribute<T, LabelTextAttribute>(enumValue);
|
|
|
|
|
// return attribute.Text;
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|