【发布时间】:2020-01-30 23:29:27
【问题描述】:
我想获取一个类中包含的所有属性,该类的类型继承自某个抽象和泛型类。
public abstract class foo<T> { }
public class fooInt_Indexed : foo<int> { }
public class fooInt_Not_Indexed : foo<int> { }
public class fooString_Compressed : foo<string> { }
public class fooString_Indexed : foo<string> { }
public class fooFloat : foo<float> { }
public abstract class bar
{
}
public class foobar : bar
{
public fooInt_Indexed value { get; set; }
public fooInt_Not_Indexed someOtherValue { get; set; }
public fooFloat someFloat { get; set; }
public otherData<int> {get; set; }
}
public class barChecker<T> where T : bar
{
public List<PropertyInfo> fooprops = new List<PropertyInfo>();
public static barChecker<T> Generator()
{
var @new = new barChecker<T>();
foreach (var item in typeof(T).GetProperties())
{
if (item.PropertyType is somesortof(foo<>)) @new.fooprops.Add(item);
}
return @new;
}
当生成为barChecker<foobar> 时,我需要在barChecker<T> 类代码中添加什么以使其 fooprops 列表包含“value”、“someOtherValue”和“someFloat”的属性信息?
【问题讨论】:
标签: c# generics properties abstract system.reflection