Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
PublicMemberInfoExtension.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 
5 namespace Entitas {
6 
7  public static class PublicMemberInfoExtension {
8 
9  public static List<PublicMemberInfo> GetPublicMemberInfos(this Type type) {
10  const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
11 
12  var fieldInfos = type.GetFields(bindingFlags);
13  var propertyInfos = type.GetProperties(bindingFlags);
14  var memberInfos = new List<PublicMemberInfo>(
15  fieldInfos.Length + propertyInfos.Length
16  );
17 
18  for(int i = 0; i < fieldInfos.Length; i++) {
19  memberInfos.Add(new PublicMemberInfo(fieldInfos[i]));
20  }
21 
22  for(int i = 0; i < propertyInfos.Length; i++) {
23  var propertyInfo = propertyInfos[i];
24  if(propertyInfo.CanRead && propertyInfo.CanWrite && propertyInfo.GetIndexParameters().Length == 0) {
25  memberInfos.Add(new PublicMemberInfo(propertyInfo));
26  }
27  }
28 
29  return memberInfos;
30  }
31 
32  public static object PublicMemberClone(this object obj) {
33  var clone = Activator.CreateInstance(obj.GetType());
34  CopyPublicMemberValues(obj, clone);
35  return clone;
36  }
37 
38  public static T PublicMemberClone<T>(this object obj) where T : new() {
39  var clone = new T();
40  CopyPublicMemberValues(obj, clone);
41  return clone;
42  }
43 
44  public static void CopyPublicMemberValues(this object source, object target) {
45  var memberInfos = source.GetType().GetPublicMemberInfos();
46  for(int i = 0; i < memberInfos.Count; i++) {
47  var info = memberInfos[i];
48  info.SetValue(target, info.GetValue(source));
49  }
50  }
51  }
52 }