更新时间:2023-12-06 11:06:52
令人惊讶,似乎要做到这一点是通过反射的唯一途径。
Surprisingly, it seems the only way to do this is via reflection.
private IEnumerable<Component> EnumerateComponents()
{
return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
where typeof (Component).IsAssignableFrom(field.FieldType)
let component = (Component) field.GetValue(this)
where component != null
select component;
}