2 using System.Collections.Generic;
5 namespace Entitas.Serialization {
9 public readonly Type type;
10 public readonly
string name;
13 readonly FieldInfo _fieldInfo;
14 readonly PropertyInfo _propertyInfo;
19 type = _fieldInfo.FieldType;
20 name = _fieldInfo.Name;
21 attributes = getAttributes(_fieldInfo.GetCustomAttributes(
false));
27 type = _propertyInfo.PropertyType;
28 name = _propertyInfo.Name;
29 attributes = getAttributes(_propertyInfo.GetCustomAttributes(
false));
35 this.attributes = attributes;
38 public object GetValue(
object obj) {
39 return _fieldInfo != null
40 ? _fieldInfo.GetValue(obj)
41 : _propertyInfo.GetValue(obj, null);
44 public void SetValue(
object obj,
object value) {
45 if(_fieldInfo != null) {
46 _fieldInfo.SetValue(obj, value);
48 _propertyInfo.SetValue(obj, value, null);
54 for (
int i = 0; i < attributes.Length; i++) {
55 var attr = attributes[i];
56 infos[i] =
new AttributeInfo(attr, attr.GetType().GetPublicMemberInfos());
65 public readonly
object attribute;
66 public readonly List<PublicMemberInfo> memberInfos;
68 public AttributeInfo(
object attribute, List<PublicMemberInfo> memberInfos) {
69 this.attribute = attribute;
70 this.memberInfos = memberInfos;
76 public static List<PublicMemberInfo> GetPublicMemberInfos(
this Type type) {
77 const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
79 var fieldInfos = type.GetFields(bindingFlags);
80 var propertyInfos = type.GetProperties(bindingFlags);
81 var memberInfos =
new List<PublicMemberInfo>(
82 fieldInfos.Length + propertyInfos.Length
85 for (
int i = 0; i < fieldInfos.Length; i++) {
89 for (
int i = 0; i < propertyInfos.Length; i++) {
90 var propertyInfo = propertyInfos[i];
91 if(propertyInfo.CanRead && propertyInfo.CanWrite) {
99 public static object PublicMemberClone(
this object obj) {
100 var clone = Activator.CreateInstance(obj.GetType());
101 CopyPublicMemberValues(obj, clone);
105 public static T PublicMemberClone<T>(
this object obj) where T :
new() {
107 CopyPublicMemberValues(obj, clone);
111 public static void CopyPublicMemberValues(
this object source,
object target) {
112 var memberInfos = source.GetType().GetPublicMemberInfos();
113 for (
int i = 0; i < memberInfos.Count; i++) {
114 var info = memberInfos[i];
115 info.SetValue(target, info.GetValue(source));