1
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");
}
我想知道是否類Q的構件(即,B類)是由反射與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)
//{}
}
}
然後我想遍歷B的繼承屬性。
我該怎麼做?我是新來的反思...
看看http://stackoverflow.com/questions/8699053/how-to-check-if-a-class-inherits-another-class-without-instantiating-it你如何檢查如果一個類可以轉換爲另一種類型。 – PhonicUK 2012-08-09 10:39:17
@ PhonicUK你給的參考,使用兩種已知的類型來檢查,但在我的情況下,派生類的類型是未知的,我試圖通過反射來找到它。 – dotNETbeginner 2012-08-09 11:24:36