public CollectionInformation(Type type) { var collectionType = EnumerableCollection.BaseCollectionType(type); directFields = collectionType.GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); var foundNames = new List <string>(directFields.Length); foreach (var targetField in directFields) { string lastName = targetField.Name; if (targetField.FieldType.Name == "T") { foundNames.Add(lastName); } else if (typeof(EnumerableCollection).IsAssignableFrom(targetField.FieldType)) { var information = EnumerableCollection.GetReflectionInformation(targetField.FieldType); foreach (var childInfo in information.directFields) { if (childInfo.FieldType.Name == "T") { foundNames.Add(lastName + "/" + childInfo.Name); } } } } fieldNames = foundNames.ToArray(); }
public static CollectionInformation GetReflectionInformation(Type type) { Type collectionType = EnumerableCollection.BaseCollectionType(type); CollectionInformation information; bool result = ReflectionCache.TryGetValue(collectionType, out information); if (!result) { information = new CollectionInformation(collectionType); ReflectionCache.Add(collectionType, information); } return(information); }
private T[] FindAllSockets <T> () where T : Socket { List <T> foundObjects = new List <T> (); for (int i = 0; i < CollectionFields.Length; i++) { FieldInfo targetField = CollectionFields[i]; if (typeof(T).IsAssignableFrom(targetField.FieldType)) { T childSocket = (T)targetField.GetValue(this); if (childSocket == null) { childSocket = (T)Activator.CreateInstance(targetField.FieldType); targetField.SetValue(this, childSocket); } childSocket.SocketName = targetField.Name; childSocket.ParentNode = this; foundObjects.Add(childSocket); } else if (typeof(IEnumerable <T>).IsAssignableFrom(targetField.FieldType)) { IEnumerable <T> childCollection = (IEnumerable <T>)targetField.GetValue(this); if (childCollection == null) { if (childCollection == null) { continue; } } foreach (var childSocket in childCollection) { if (childSocket == null) { continue; } childSocket.SocketName = targetField.Name; childSocket.ParentNode = this; foundObjects.Add(childSocket); } } else if (typeof(EnumerableCollection <T>).IsAssignableFrom(targetField.FieldType)) { if (!typeof(T).IsAssignableFrom(targetField.FieldType.BaseType.GetGenericArguments()[0])) { continue; } EnumerableCollection <T> enumer = (EnumerableCollection <T>)targetField.GetValue(this); if (enumer == null) { continue; } foreach (object child in enumer) { if (child == null) { continue; } if (typeof(T).IsAssignableFrom(child.GetType())) { T childSocket = (T)child; if (childSocket == null) { childSocket = (T)Activator.CreateInstance(targetField.FieldType); targetField.SetValue(this, childSocket); } childSocket.SocketName = targetField.Name; childSocket.ParentNode = this; foundObjects.Add(childSocket); } } } } return(foundObjects.ToArray()); }