Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
PublicMemberInfo.cs
1 using System;
2 using System.Reflection;
3 
4 namespace Entitas {
5 
6  public class PublicMemberInfo {
7 
8  public readonly Type type;
9  public readonly string name;
10  public readonly AttributeInfo[] attributes;
11 
12  readonly FieldInfo _fieldInfo;
13  readonly PropertyInfo _propertyInfo;
14 
15  public PublicMemberInfo(FieldInfo info) {
16  _fieldInfo = info;
17  type = _fieldInfo.FieldType;
18  name = _fieldInfo.Name;
19  attributes = getAttributes(_fieldInfo.GetCustomAttributes(false));
20  }
21 
22  public PublicMemberInfo(PropertyInfo info) {
23  _propertyInfo = info;
24  type = _propertyInfo.PropertyType;
25  name = _propertyInfo.Name;
26  attributes = getAttributes(_propertyInfo.GetCustomAttributes(false));
27  }
28 
29  public PublicMemberInfo(Type type, string name, AttributeInfo[] attributes = null) {
30  this.type = type;
31  this.name = name;
32  this.attributes = attributes;
33  }
34 
35  public object GetValue(object obj) {
36  return _fieldInfo != null
37  ? _fieldInfo.GetValue(obj)
38  : _propertyInfo.GetValue(obj, null);
39  }
40 
41  public void SetValue(object obj, object value) {
42  if(_fieldInfo != null) {
43  _fieldInfo.SetValue(obj, value);
44  } else {
45  _propertyInfo.SetValue(obj, value, null);
46  }
47  }
48 
49  static AttributeInfo[] getAttributes(object[] attributes) {
50  var infos = new AttributeInfo[attributes.Length];
51  for(int i = 0; i < attributes.Length; i++) {
52  var attr = attributes[i];
53  infos[i] = new AttributeInfo(attr, attr.GetType().GetPublicMemberInfos());
54  }
55 
56  return infos;
57  }
58  }
59 }