using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace RelationsInspector.Backend.AutoBackend { public class ReflectionUtil { public static IEnumerable GetAttributeFields() { return typeof( P ) .GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ) .Where( fInfo => HasAttribute( fInfo, typeof( S ) ) ); } static bool HasAttribute( MemberInfo mInfo, Type attrType ) { return mInfo.GetCustomAttributes( attrType, false ).Any(); } public static IEnumerable GetValues( FieldInfo fInfo, T fObj ) where T : class { // how to get T objects out of different field types var extractValues = new Dictionary>>() { {typeof(T), obj => new T[] { obj as T } }, {typeof(List), obj => ( obj as List ) ?? Enumerable.Empty() }, {typeof(T[]), obj => ( obj as T[] ) ?? Enumerable.Empty() } }; if ( !extractValues.ContainsKey( fInfo.FieldType ) ) return Enumerable.Empty(); return extractValues[ fInfo.FieldType ] .Invoke( fInfo.GetValue( fObj ) ) .Where( v => v != null ); } } }