C# 检查类型是否与父级兼容,然后通过反射遍历其属性
我想知道Q类(即B类)的成员是否通过反射与A类兼容C# 检查类型是否与父级兼容,然后通过反射遍历其属性,c#,inheritance,reflection,C#,Inheritance,Reflection,我想知道Q类(即B类)的成员是否通过反射与A类兼容 class A { public string proprt1 { get; set; } public string proprt2 { get; set; } public A(string p1,string p2) { proprt1 = p1; proprt2 = p2; } } class B : A { public B(string p1,str
class A
{
public string proprt1 { get; set; }
public string proprt2 { get; set; }
public A(string p1,string p2)
{
proprt1 = p1;
proprt2 = p2;
}
}
class B : A
{
public B(string p1,string p2):base(p1,p2)
{
}
}
class Q
{
public B b = new B("a","b");
}
然后我想遍历B.的继承属性
我该怎么做?我不熟悉反射…这在我的测试应用程序中有效
private void someMethod()
{
Q q = new Q();
Type type = q.GetType();
foreach (FieldInfo t in type.GetFields())
{
//I am stuck here
//if (t.GetType() is A)
//{}
}
}
这在我的测试应用程序中有效
private void someMethod()
{
Q q = new Q();
Type type = q.GetType();
foreach (FieldInfo t in type.GetFields())
{
//I am stuck here
//if (t.GetType() is A)
//{}
}
}
查看如何检查一个类是否可以转换为另一种类型。@PhonicUK您提供的引用使用了两种已知类型进行检查,但在我的情况下,派生类类型未知,我试图通过反射找到它。。查看如何检查一个类是否可以转换为另一种类型。@PhonicUK您提供的引用,使用两个已知类型进行检查,但在我的示例中,派生类类型未知,我正在尝试通过反射查找它。。