Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查类型是否与父级兼容,然后通过反射遍历其属性_C#_Inheritance_Reflection - Fatal编程技术网

C# 检查类型是否与父级兼容,然后通过反射遍历其属性

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

我想知道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,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您提供的引用,使用两个已知类型进行检查,但在我的示例中,派生类类型未知,我正在尝试通过反射查找它。。