5using System.Reflection;
6using System.Collections.Generic;
8using Newtonsoft.Json.Linq;
18 public static class pb_Reflection
23 public static IEnumerable<PropertyInfo> GetSerializableProperties(Type type, BindingFlags flags)
30 return type.GetProperties(flags).Where(
32 ( (flags & BindingFlags.Public) == 0 || x.GetSetMethod() !=
null ) &&
34 !HasIgnoredAttribute(x) );
40 public static IEnumerable<FieldInfo> GetSerializableFields(Type type, BindingFlags flags)
42 return type.GetFields(flags).Where(x =>
43 ( (flags & BindingFlags.Public) == 0 || !x.IsPrivate ) &&
44 !HasIgnoredAttribute(x) );
51 public static Dictionary<string, object> ReflectProperties<T>(T obj)
53 return ReflectProperties<T>(obj, BindingFlags.Instance | BindingFlags.Public,
null);
56 public static Dictionary<string, object> ReflectProperties<T>(T obj, HashSet<string> ignoreFields)
58 return ReflectProperties<T>(obj, BindingFlags.Instance | BindingFlags.Public, ignoreFields);
61 public static Dictionary<string, object> ReflectProperties<T>(T obj, BindingFlags flags, HashSet<string> ignoreFields)
64 System.Text.StringBuilder sb =
new System.Text.StringBuilder();
65 sb.AppendLine(
"Component type(" + obj.GetType().ToString() +
")");
68 Dictionary<string, object> properties =
new Dictionary<string, object>();
70 Type type = obj.GetType();
72 foreach(PropertyInfo prop
in pb_Reflection.GetSerializableProperties(type, flags))
76 bool willWrite = prop.CanWrite &&
77 !prop.IsSpecialName &&
78 !HasIgnoredAttribute(prop) &&
79 !(ignoreFields !=
null && ignoreFields.Contains(prop.Name));
82 sb.AppendLine((willWrite ?
"p - " :
"px- ") + prop.Name +
" : " + prop.GetValue(obj,
null));
87 ParameterInfo[] parms = prop.GetIndexParameters();
89 if(parms !=
null && parms.Length > 0)
95 properties.Add(prop.Name, prop.GetValue(obj, parms));
102 foreach(FieldInfo field
in type.GetFields(flags) )
107 sb.AppendLine((HasIgnoredAttribute(field) ?
"fx - " :
"f - ") + field.Name +
" : " + field.GetValue(obj));
110 if( HasIgnoredAttribute(field) ||
111 (ignoreFields !=
null && ignoreFields.Contains(field.Name)))
114 properties.Add(field.Name, field.GetValue(obj));
117 Debug.LogError(
"Failed extracting property: " + field.Name);
122 Debug.Log(sb.ToString());
131 public static T GetValue<T>(
object obj,
string name)
133 return GetValue<T>(obj, name, BindingFlags.Instance | BindingFlags.Public);
139 public static T GetValue<T>(
object obj,
string name, BindingFlags flags)
144 PropertyInfo prop = obj.GetType().GetProperty(name, flags);
147 return (T) prop.GetValue(obj,
null);
149 FieldInfo field = obj.GetType().GetField(name, flags);
152 return (T) field.GetValue(obj);
157 public static bool SetValue(
object obj,
string name,
object value)
159 return SetValue(obj, name, value, BindingFlags.Instance | BindingFlags.Public);
162 public static bool SetValue(
object obj,
string name,
object value, BindingFlags flags)
167 PropertyInfo prop = obj.GetType().GetProperty(name, flags);
170 return SetPropertyValue(obj, prop, value);
172 FieldInfo field = obj.GetType().GetField(name, flags);
175 return SetFieldValue(obj, field, value);
185 public static bool SetPropertyValue(
object target, PropertyInfo propertyInfo,
object value)
187 if(propertyInfo ==
null || target ==
null)
192 var val = value is JToken ? ((JToken)value).ToObject(propertyInfo.PropertyType, pb_Serialization.Serializer) : value;
194 if(propertyInfo.PropertyType.IsEnum)
196 int conv = (int) Convert.ChangeType(val, typeof(
int));
197 propertyInfo.SetValue(target, conv,
null);
203 var conv = Convert.ChangeType(actual, propertyInfo.PropertyType);
204 propertyInfo.SetValue(target, conv,
null);
208 var conv = Convert.ChangeType(val, propertyInfo.PropertyType);
209 propertyInfo.SetValue(target, conv,
null);
215 catch(System.Exception e)
217 Debug.LogWarning(e.ToString());
232 public static bool SetFieldValue(
object target, FieldInfo field,
object value)
234 if(target ==
null || field ==
null)
239 var val = value is JToken ? ((JToken)value).ToObject(field.FieldType, pb_Serialization.Serializer) : value;
241 if(field.FieldType.IsEnum)
243 int conv = (int) Convert.ChangeType(val, typeof(
int));
244 field.SetValue(target, conv);
250 var conv = Convert.ChangeType(actual, field.FieldType);
251 field.SetValue(target, conv);
255 var conv = Convert.ChangeType(val, field.FieldType);
256 field.SetValue(target, conv);
261 catch(System.Exception e)
263 Debug.LogError(e.ToString());
271 public static void ApplyProperties<T>(T obj, Dictionary<string, object> properties)
273 foreach(KeyValuePair<string, object> kvp
in properties)
275 SetValue(obj, kvp.Key, kvp.Value);
279 public static readonly HashSet<System.Type> IgnoreAttributes =
new HashSet<System.Type>
281 typeof(System.ObsoleteAttribute),
288 public static bool HasIgnoredAttribute(Type type)
290 return type.GetCustomAttributes(
true).Any(x => IgnoreAttributes.Contains(x.GetType()));
293 public static bool HasIgnoredAttribute<T>(T info) where T : MemberInfo
295 return info.GetCustomAttributes(
true).Any(x => IgnoreAttributes.Contains(x.GetType()));
298 public static IEnumerable<Attribute> GetAttributes<T>(T obj)
300 return (IEnumerable<Attribute>) typeof(T).GetCustomAttributes(
true);