Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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#_Serialization_Reflection - Fatal编程技术网

C# 使用反射显示来自不同自定义类的数据

C# 使用反射显示来自不同自定义类的数据,c#,serialization,reflection,C#,Serialization,Reflection,我想创建一个方法来显示对象中包含的信息,该方法可以动态地处理任何对象。我在处理其他自定义类的属性时遇到问题。在下面的示例中,人员拥有电话和职业,这两个职业都是其他类别。显示数据时,屏幕上当前的值为: TestReflection.Person Name: Mary Phones: TestReflection.Phones Occupations: TestReflection.Occupations 它只显示类的名称,比如TestReflection.Phones,而不是该对象中的数据 如何

我想创建一个方法来显示对象中包含的信息,该方法可以动态地处理任何对象。我在处理其他自定义类的属性时遇到问题。在下面的示例中,
人员
拥有
电话
职业
,这两个职业都是其他类别。显示数据时,屏幕上当前的值为:

TestReflection.Person
Name: Mary
Phones: TestReflection.Phones
Occupations: TestReflection.Occupations
它只显示类的名称,比如
TestReflection.Phones
,而不是该对象中的数据

如何更改此代码以显示这样的信息

TestReflection.Person
Name: Mary
Phones:
    TestReflection.Phones
    Type: 1
    Number: 555XYZ
Occupations: 
    TestReflection.Occupations
    Type: 5
    Description: Secretary
这是我的密码:

class Program
{
    static void Main(string[] args)
    {
        List<Person> listPeson = new List<Person>();
        var person1 = new Person();
        person1.Name = "Mary";
        person1.Phones = new  Phones { new Phone { Type = 1, Number = "555XYZ" } };
        person1.Occupations = new Occupations {new Occupation { Type = 5, Description = "Secretary" }};
        listPeson.Add(person1);
        DynamicExport(listPeson);
        Console.ReadLine();
    }

    public static void DynamicExport<T>(List<T> listReg)
    {

        for (int i = 0; i < listReg.Count; i++)
        {
            Console.WriteLine(listReg[i].GetType());
            foreach (var item in listReg[i].GetType().GetProperties())
            {
                Console.WriteLine($"{item.Name}: {item.GetValue(listReg[i], null)}");
            }
        }
    }
}


class Person
{
    public string Name { get; set; }
    public Phones Phones { get; set; }
    public Occupations Occupations { get; set; }
}

class Phones : List<Phone> { }
class Phone
{
    public int Type { get; set; }
    public string Number { get; set; }
}

class Occupations : List<Occupation> { }
class Occupation

{
    public int Type { get; set; }
    public string Description { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
List listPeson=新列表();
var person1=新人();
person1.Name=“玛丽”;
person1.Phones=newphones{newphone{Type=1,Number=“555XYZ”};
person1.职业=新职业{新职业{Type=5,Description=“Secretary”};
listPeson.Add(person1);
动态导出(listPeson);
Console.ReadLine();
}
公共静态void DynamicExport(列表listReg)
{
对于(int i=0;i
我对你的问题做了一些修改-我希望我理解正确

如果要导出数据 如果您的问题真的是关于显示数据,那么有比创建自己的导出方法更好的方法。您试图显示的格式与YAML类似。还有JSON和XML。使用其中一个库可能比编写自己的方法要好:

如果你想了解更多关于反思的知识 也许您对学习更多关于反射的知识感兴趣,而导出只是一个示例。在这种情况下,让我们看看这一行:

Console.WriteLine($"{item.Name}: {item.GetValue(listReg[i], null)}");
$“{item.GetValue(listReg[i],null)}”
最后调用
person1.Phones.ToString()。
ToString
的默认行为仅显示类型名称。您可以覆盖该行为,如下所示:

class Phones : List<Phone>
{
    public override string ToString()
    {
        return Program.DynamicExportToString(this);

        // ... where DynamicExportToString is a modified version of DynamicExport that
        // builds and returns a string rather than sending it directly to the Console.
    }
}
。。。并不是每种情况都适用。我们需要根据属性的类型显示不同的内容

  • 考虑如何处理空值。可能类似于
    $“{item.Name}:”
  • 如果类型为。。。
    • a
    • DateTime
    • String
    • 。。。或者这些类型之一的
      可为空的
  • 如果类型实现了
    IEnumerable
    ,则循环集合的内容并递归调用每个元素的导出代码。
    • 在检查类型是否为
      String
      之后,检查此接口非常重要,因为
      String
      实现了
      IEnumerable
  • 否则,递归调用此值的导出代码
当您递归调用导出代码时,最好防止出现无限循环。如果您试图导出的对象包含循环引用,则很快就会出现循环引用。要避免这种情况,请维护已访问的对象堆栈

我认为上述建议通常适用于使用反射遍历对象图的任何时候——无论是用于序列化还是任何其他目的。 我希望这有帮助

$"{item.Name}: {item.GetValue(listReg[i], null)}"