Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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中读取嵌套类的元素#_C#_Recursion - Fatal编程技术网

C# 在C中读取嵌套类的元素#

C# 在C中读取嵌套类的元素#,c#,recursion,C#,Recursion,我必须遍历一个类的所有成员并在树中显示它们。现在可以忽略树处理,此时我只需要在一个简单的列表中获取所有类成员就可以了 我能找到的所有示例(例如)仅返回以下类的前两个成员cTest: public class cTest { public String str1; public int intwert1; public cParent Parent = new cParent(); } public class cParent { public String pa

我必须遍历一个类的所有成员并在树中显示它们。现在可以忽略树处理,此时我只需要在一个简单的列表中获取所有类成员就可以了

我能找到的所有示例(例如)仅返回以下类的前两个成员
cTest

public class cTest
{
    public String str1;
    public int intwert1;
    public cParent Parent = new cParent();
}

public class cParent
{
    public String parentStr1;
}
未找到字段
public cParent Parent
,并且未递归处理该字段。对于以下代码,我已经可以看出:

Type t = typeof(cTest);
PropertyInfo[] propertyInfos;
MemberInfo[] propertyMembers;
propertyInfos = t.GetProperties();
propertyMembers = t.GetMembers();
propertyInfos
不包含
public cParent父项
,但
propertyMembers
包含
public cParent父项

编辑

thx,但仍然卡住了。下面的代码为所有类成员提供相同的信息

        foreach (FieldInfo p in t.GetFields())
        {
            Console.WriteLine("-");
            Console.WriteLine(p.GetType().ToString());
            Console.WriteLine(p.Name.ToString());
            Console.WriteLine(p.MemberType.GetType().ToString());
            Console.WriteLine(p.MemberType.GetTypeCode().ToString());
            Console.WriteLine(p.MemberType.ToString());
        }  
输出: ^

GetProperties()返回类的属性。父项不是属性,而是字段。GetMembers()返回所有成员(属性、字段、方法等),这就是它显示在propertyMembers中的原因。尝试使用GetFields()代替GetProperties()

编辑以回答其他问题:

您发布的代码正确地给出了每个字段的名称。它为每个字段返回System.Reflection.MemberTypes和Int32是因为您没有对字段的类型调用GetType()和GetTypeCode()。您在属性上调用它们,该属性是该类型的枚举(是Int32枚举),因此对所有字段都是相同的

您需要在返回字段类型的属性上调用这些方法。不在p.MemberType属性上,该属性返回成员是否为方法、字段、属性等

所以你的代码应该是这样的:

foreach (FieldInfo p in t.GetFields())
{
    Console.WriteLine("-");
    Console.WriteLine(p.GetType().ToString());
    Console.WriteLine(p.Name.ToString());
    Console.WriteLine(p.FieldType.GetType().ToString());
    Console.WriteLine(p.FieldType.GetTypeCode().ToString());
    Console.WriteLine(p.FieldType.ToString());
}  

要是我们知道你在说什么就好了……试着用谷歌翻译,但还是听不懂。对不起,伙计。这是谷歌和谷歌的合作伙伴。GetProperties()返回类的属性。父项不是属性,而是字段。GetMembers()返回所有成员(属性、字段、方法等),这就是它显示在propertyMembers中的原因。尝试使用GetFields()代替GetProperties()。@Bas,添加为答案。“你们应该得到一张向上的选票。”蒂姆施梅尔特、阿什和史蒂夫,既然是英国人,请重新开张。
foreach (FieldInfo p in t.GetFields())
{
    Console.WriteLine("-");
    Console.WriteLine(p.GetType().ToString());
    Console.WriteLine(p.Name.ToString());
    Console.WriteLine(p.FieldType.GetType().ToString());
    Console.WriteLine(p.FieldType.GetTypeCode().ToString());
    Console.WriteLine(p.FieldType.ToString());
}