123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- using System.Collections;
- using CommonLang.Log;
- namespace CommonLang.Property
- {
- public static class PropertyUtil
- {
- private static Logger s_log;
- public static Logger log { get { if (s_log == null) { s_log = LoggerFactory.GetLogger(typeof(PropertyUtil).Name); } return s_log; } }
- static public FieldInfo[] SortFields(FieldInfo[] fields)
- {
- Array.Sort(fields, BaseFieldSorter);
- return fields;
- }
- static public PropertyInfo[] SortProperties(PropertyInfo[] properties)
- {
- Array.Sort(properties, BasePropertySorter);
- return properties;
- }
- static public IComparer<FieldInfo> BaseFieldSorter = new FieldSorter();
- static public IComparer<PropertyInfo> BasePropertySorter = new PropertySorter();
- public class FieldSorter : IComparer<FieldInfo>
- {
- public int Compare(FieldInfo x, FieldInfo y)
- {
- return x.Name.CompareTo(y.Name);
- }
- }
- public class PropertySorter : IComparer<PropertyInfo>
- {
- public int Compare(PropertyInfo x, PropertyInfo y)
- {
- return x.Name.CompareTo(y.Name);
- }
- }
- public static Attribute GetAttributeByType(FieldInfo field, Type attributeType)
- {
- object[] cas = field.GetCustomAttributes(attributeType, false);
- if (cas != null && cas.Length > 0)
- {
- return cas[0] as Attribute;
- }
- return null;
- }
- public static T GetAttribute<T>(Type type) where T : System.Attribute
- {
- while (!type.Equals(typeof(object)))
- {
- object[] cas = type.GetCustomAttributes(typeof(T), false);
- if (cas != null && cas.Length > 0)
- {
- return (T)cas[0];
- }
- else if (type.BaseType != null)
- {
- type = type.BaseType;
- }
- else { return null; }
- }
- return null;
- }
- public static T GetAttribute<T>(MemberInfo member) where T : System.Attribute
- {
- object[] cas = member.GetCustomAttributes(typeof(T), false);
- if (cas != null && cas.Length > 0)
- {
- return (T)cas[0];
- }
- return null;
- }
- public static T GetAttribute<T>(FieldInfo field) where T : System.Attribute
- {
- object[] cas = field.GetCustomAttributes(typeof(T), false);
- if (cas != null && cas.Length > 0)
- {
- return (T)cas[0];
- }
- return null;
- }
- public static T GetAttribute<T>(PropertyInfo property) where T : System.Attribute
- {
- object[] cas = property.GetCustomAttributes(typeof(T), false);
- if (cas != null && cas.Length > 0)
- {
- return (T)cas[0];
- }
- return null;
- }
- static public T GetEnumAttribute<T>(object value) where T : Attribute
- {
- Type type = value.GetType();
- string name = Enum.GetName(type, value);
- if (name == null)
- {
- return null;
- }
- FieldInfo field = type.GetField(name);
- if (field != null)
- {
- return GetAttribute<T>(field);
- }
- return null;
- }
- public static DescAttribute GetDesc(Type type)
- {
- return GetAttribute<DescAttribute>(type);
- }
- public static DescAttribute GetDesc(MemberInfo field)
- {
- return GetAttribute<DescAttribute>(field);
- }
- public static ListAttribute GetListDesc(MemberInfo field)
- {
- return GetAttribute<ListAttribute>(field);
- }
- /// <summary>
- /// 将一个对象里面所有的Attribute的标记的Field值,全部取出
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="data"></param>
- /// <param name="collection"></param>
- /// <returns></returns>
- static public void CollectFieldTypeValues<T>(object data, List<T> collection)
- {
- if (data != null)
- {
- Type fieldType = typeof(T);
- Type type = data.GetType();
- if (data is T)
- {
- collection.Add((T)data);
- }
- if (type.IsClass && !type.IsPrimitive)
- {
- if (data is IDictionary)
- {
- IDictionary map = data as IDictionary;
- foreach (object o in map.Values)
- {
- CollectFieldTypeValues<T>(o, collection);
- }
- }
- else if (data is ICollection)
- {
- ICollection list = data as ICollection;
- foreach (object o in list)
- {
- CollectFieldTypeValues<T>(o, collection);
- }
- }
- else if (type.IsArray)
- {
- Array array = (Array)data;
- foreach (object o in array)
- {
- CollectFieldTypeValues<T>(o, collection);
- }
- }
- else
- {
- foreach (FieldInfo field in type.GetFields())
- {
- if (!field.IsStatic)
- {
- object fd = field.GetValue(data);
- if (fd != null)
- {
- CollectFieldTypeValues<T>(fd, collection);
- }
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 将一个对象里面所有的Attribute的标记的Field值,全部取出
- /// </summary>
- /// <param name="data"></param>
- /// <param name="attributeType"></param>
- /// <param name="collection"></param>
- static public void CollectFieldAttributeValues(object data, Type attributeType, List<FieldAttributeValue> collection)
- {
- if (data != null)
- {
- Type type = data.GetType();
- if (data is IDictionary)
- {
- IDictionary map = data as IDictionary;
- foreach (object o in map.Values)
- {
- CollectFieldAttributeValues(o, attributeType, collection);
- }
- }
- else if (data is ICollection)
- {
- ICollection list = data as ICollection;
- foreach (object o in list)
- {
- CollectFieldAttributeValues(o, attributeType, collection);
- }
- }
- else if (type.IsArray)
- {
- Array array = (Array)data;
- foreach (object o in array)
- {
- CollectFieldAttributeValues(o, attributeType, collection);
- }
- }
- else if (type.IsClass)
- {
- foreach (FieldInfo field in type.GetFields())
- {
- if (!field.IsStatic)
- {
- object fd = field.GetValue(data);
- if (fd != null)
- {
- Attribute attr = PropertyUtil.GetAttributeByType(field, attributeType);
- if (attr != null)
- {
- FieldAttributeValue fv = new FieldAttributeValue(field, attr, fd, data);
- collection.Add(fv);
- }
- else
- {
- CollectFieldAttributeValues(fd, attributeType, collection);
- }
- }
- }
- }
- }
- }
- }
- static public void CollectFieldAttributeValuesGeneric<A, T>(object data, List<TFieldAttributeValue<A, T>> collection) where A : Attribute
- {
- List<FieldAttributeValue> list = new List<FieldAttributeValue>();
- CollectFieldAttributeValues(data, typeof(A), list);
- foreach (FieldAttributeValue fv in list)
- {
- collection.Add(new TFieldAttributeValue<A, T>(fv.Field, fv.AttributeData as A, fv.FieldValue, fv.FieldOwner));
- }
- }
- /// <summary>
- /// 返回书写习惯的类型字符串
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static string ToTypeDefineString(Type type)
- {
- if (type.IsGenericType)
- {
- StringBuilder sb = new StringBuilder();
- int tidx = type.Name.IndexOf('`');
- if (tidx > 0)
- {
- sb.Append(type.Name.Substring(0, tidx));
- }
- else
- {
- sb.Append(type.Name);
- }
- sb.Append("<");
- Type[] g_args = type.GetGenericArguments();
- foreach (Type g_arg in g_args)
- {
- sb.Append(ToTypeDefineString(g_arg));
- if (!g_arg.Equals(g_args[g_args.Length - 1]))
- {
- sb.Append(", ");
- }
- }
- sb.Append(">");
- return sb.ToString();
- }
- return type.Name;
- }
- public static T GetFieldOrPropertyOrMethodValue<T>(object componentData, string fieldName)
- {
- Type componentType = componentData.GetType();
- FieldInfo depend_field = componentType.GetField(fieldName);
- if (depend_field != null)
- {
- try
- {
- T ret = (T)depend_field.GetValue(componentData);
- return ret;
- }
- catch (Exception err) { log.Error(err.Message, err); }
- }
- PropertyInfo depend_property = componentType.GetProperty(fieldName);
- if (depend_property != null)
- {
- try
- {
- T ret = (T)depend_property.GetValue(componentData, null);
- return ret;
- }
- catch (Exception err) { log.Error(err.Message, err); }
- }
- MethodInfo depend_method = componentType.GetMethod(fieldName);
- if (depend_method != null)
- {
- try
- {
- T ret = (T)depend_method.Invoke(componentData, ZERO_ARGS);
- return ret;
- }
- catch (Exception err) { log.Error(err.Message, err); }
- }
- return default(T);
- }
- public readonly static object[] ZERO_ARGS = new object[] { };
- public static void SetMemberValue(MemberInfo field, object obj, object value)
- {
- if (field is FieldInfo)
- {
- (field as FieldInfo).SetValue(obj, value);
- }
- else if (field is PropertyInfo)
- {
- var set = (field as PropertyInfo).GetSetMethod();
- if (set != null)
- {
- set.Invoke(obj, new object[] { value });
- }
- }
- }
- public static object GetMemberValue(MemberInfo field, object obj)
- {
- if (field is FieldInfo)
- {
- return (field as FieldInfo).GetValue(obj);
- }
- else if (field is PropertyInfo)
- {
- var get = (field as PropertyInfo).GetGetMethod();
- if (get != null)
- {
- return get.Invoke(obj, ZERO_ARGS);
- }
- }
- return null;
- }
- }
- public struct FieldAttributeValue
- {
- public Attribute AttributeData;
- public FieldInfo Field;
- public object FieldOwner;
- public object FieldValue;
- public FieldAttributeValue(FieldInfo field, Attribute attr, object fieldData, object fieldOwner)
- {
- this.AttributeData = attr;
- this.Field = field;
- this.FieldOwner = fieldOwner;
- this.FieldValue = fieldData;
- }
- }
- public struct TFieldAttributeValue<A, T> where A : Attribute
- {
- public A AttributeData;
- public FieldInfo Field;
- public object FieldOwner;
- public T FieldValue;
- public TFieldAttributeValue(FieldInfo field, A attr, object fieldData, object fieldOwner)
- {
- this.AttributeData = attr;
- this.Field = field;
- this.FieldOwner = fieldOwner;
- this.FieldValue = (T)fieldData;
- }
- }
- public class TypeDescAttribute : IComparable<TypeDescAttribute>
- {
- public DescAttribute Desc;
- public Type DataType;
- public TypeDescAttribute(Type type)
- {
- DataType = type;
- Desc = PropertyUtil.GetAttribute<DescAttribute>(type);
- }
- public override string ToString()
- {
- return Desc.Desc;
- }
- public int CompareTo(TypeDescAttribute other)
- {
- return this.ToString().CompareTo(other.ToString());
- }
- /// <summary>
- /// 如果此类型有DescAttribute签名,则返回Desc
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static string GetDescText(Type type)
- {
- DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(type);
- if (desc != null)
- {
- return desc.Desc;
- }
- return "";
- }
- /// <summary>
- /// 如果此类型有DescAttribute签名,则返回Catgory
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static string GetCatgoryName(Type type)
- {
- DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(type);
- if (desc != null)
- {
- return desc.Category;
- }
- return "";
- }
- }
- public class MemberDescAttribute : IComparable<MemberDescAttribute>
- {
- public readonly DescAttribute Desc;
- public readonly MemberInfo DataField;
- public readonly Type FieldType;
- public MemberDescAttribute(MemberInfo field)
- {
- DataField = field;
- Desc = PropertyUtil.GetAttribute<DescAttribute>(field);
- if (field is FieldInfo)
- {
- FieldType = (field as FieldInfo).FieldType;
- }
- else if (field is PropertyInfo)
- {
- FieldType = (field as PropertyInfo).PropertyType;
- }
- else if (field is MethodInfo)
- {
- FieldType = (field as MethodInfo).ReturnType;
- }
- else
- {
- throw new Exception("Error MemberDescAttribute : " + field);
- }
- }
- public string ToDescString()
- {
- return (Desc != null ? string.Format("[{0}]", Desc.Desc) : "");
- }
- public override string ToString()
- {
- if (DataField is MethodInfo)
- {
- return string.Format("{0}() {1}", DataField.Name, ToDescString());
- }
- else
- {
- return string.Format("{0} {1}", DataField.Name, ToDescString());
- }
- }
- public int CompareTo(MemberDescAttribute other)
- {
- return DataField.Name.CompareTo(other.DataField.Name);
- }
- public object GetValue(object owner)
- {
- try
- {
- if (DataField is FieldInfo)
- {
- FieldInfo field = DataField as FieldInfo;
- return field.GetValue(owner);
- }
- else if (DataField is PropertyInfo)
- {
- PropertyInfo field = DataField as PropertyInfo;
- return field.GetGetMethod().Invoke(owner, new object[0]);
- }
- else if (DataField is MethodInfo)
- {
- MethodInfo method = DataField as MethodInfo;
- return method.Invoke(owner, new object[0]);
- }
- }
- catch (Exception err) { PropertyUtil.log.Error(err.Message, err); }
- return null;
- }
- public void SetValue(object owner, object value)
- {
- try
- {
- if (DataField is FieldInfo)
- {
- FieldInfo field = DataField as FieldInfo;
- field.SetValue(owner, value);
- }
- else if (DataField is PropertyInfo)
- {
- PropertyInfo field = DataField as PropertyInfo;
- field.GetSetMethod().Invoke(owner, new object[] { value });
- }
- else if (DataField is MethodInfo)
- {
- MethodInfo method = DataField as MethodInfo;
- method.Invoke(owner, new object[] { value });
- }
- }
- catch (Exception err) { PropertyUtil.log.Error(err.Message, err); }
- }
- }
- }
|