特定インスタンスのフィールドに指定型が含まれているか調べるコードです。
フィールドの中のフィールドも調べます。
試した感じですが, フィールドを省略してプロパティを定義していても取得できるっぽかったです。
↓みたいなやつ
public Hoge Hoge { get; set; }
ソースコード
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; public static class ReflectionHelper { private static Dictionary<Type, CacheData> Cache = new(); private static CacheData SafeGetCache<T>() { if (!Cache.TryGetValue(typeof(T), out var cacheData)) { cacheData = new CacheData(); Cache.Add(typeof(T), cacheData); } return cacheData; } /// <summary> /// instance の持つ T 型の インスタンス を全て(T, List<T>, T[] が対象)取得する. /// </summary> public static List<T> GetAllTInstancies<T>(object instance) where T : class { var result = new List<T>(); foreach (var kv in GetInstanceToFieldInfo<T>(instance)) { foreach (var fieldInfo in kv.Value) { var i = fieldInfo.GetValue(kv.Key); if (fieldInfo.FieldType.IsGenericType) { if (fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(List<>)) { result.AddRange((IEnumerable<T>)i); } } else if (fieldInfo.FieldType.IsArray) { result.AddRange((T[])i); } else { result.Add((T)i); } } } return result.Distinct().ToList(); } /// <summary> /// instance の T 型の FieldInfo を全て取得する. /// instance のベースクラスの FieldInfo も含みます. /// フィールドのフィールドもチェックします. /// フィールドのベースクラスのフィールドもチェックします. /// </summary> public static Dictionary<object, HashSet<FieldInfo>> GetInstanceToFieldInfo<T>(object instance) { var result = new Dictionary<object, HashSet<FieldInfo>>(); GetInstanceToFieldInfo<T>(instance, instance.GetType(), result); return result; } private static void GetInstanceToFieldInfo<T>(object instance, Type type, Dictionary<object, HashSet<FieldInfo>> result) { if (instance == null) { return; } if (type == null) { return; } if (!HasField<T>(type)) { return; } foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var fieldType = field.FieldType; if (DependenceToT<T>(fieldType, SafeGetCache<T>().SkipTypeCheckList)) { SafeAdd(instance, field); } // フィールドのベースクラスのフィールドをチェック. GetInstanceToFieldInfo<T>(field.GetValue(instance), fieldType.BaseType, result); // フィールドのフィールドをチェック. GetInstanceToFieldInfo<T>(field.GetValue(instance), fieldType, result); } return; void SafeAdd(object instance, FieldInfo fieldInfo) { if (!result.TryGetValue(instance, out var hashSet)) { hashSet = new HashSet<FieldInfo>(); result.Add(instance, hashSet); } hashSet.Add(fieldInfo); } } /// <summary> /// type の Field に T があるか. /// Field の Field を再帰的にチェックします. /// Field の ベースクラスの Field もチェックします. /// </summary> public static bool HasField<T>(Type type) { return HasField<T>(type, null); } private static bool HasField<T>(Type type, HashSet<Type> alreadyChecked) { if (DependenceToT<T>(type, SafeGetCache<T>().SkipTypeCheckList)) { return true; } alreadyChecked ??= new HashSet<Type>(); if (alreadyChecked.Contains(type)) { return false; } alreadyChecked.Add(type); foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { // フィールドのフィールドをチェック. if (HasField<T>(field.FieldType, alreadyChecked)) { return true; } } return false; } /// <summary> /// type が T に依存しているか. /// type[], List<type>, List<type>[] の場合も true を返します. /// type のベースクラスが T の場合も true を返します. /// </summary> private static bool DependenceToT<T>(Type type, HashSet<Type> skipTypeList, HashSet<Type> alreadyChecked = null) { // 無限ループ対策. A : B<A> など. alreadyChecked ??= new HashSet<Type>(); if (alreadyChecked.Contains(type)) { return false; } alreadyChecked.Add(type); if (skipTypeList.Contains(type)) { return false; } if (type == null) { return false; } if (type == typeof(object)) { return false; } if (type == typeof(T)) { return true; } if (type.IsGenericType) { foreach (var arg in type.GetGenericArguments()) { if (arg == typeof(T)) { return true; } if (DependenceToT<T>(arg, skipTypeList, alreadyChecked)) { return true; } else { skipTypeList.Add(arg); } } } if (type.IsArray) { var elementType = type.GetElementType(); if (elementType == typeof(T)) { return true; } type = elementType; } if (DependenceToT<T>(type.BaseType, skipTypeList, alreadyChecked)) { return true; } skipTypeList.Add(type); return false; } private class CacheData { public HashSet<Type> SkipTypeCheckList = new HashSet<Type>(); } }