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

C# 获取嵌套属性值

C# 获取嵌套属性值,c#,C#,我有一个有25个属性的类。其中一些属性是自定义对象的列表 e、 g.类A对象中的X3属性是类B对象的列表 class A { public int X1 { get; set; } public string X2 { get; set; } public List<B> X3 { get; set; } } 迭代类A对象的属性以获得属性的非空值和列表中任何嵌套对象的属性的最佳方法是什么?您可以使用.NET的内置反射API迭代类型的属性。快速和肮脏的例

我有一个有25个属性的类。其中一些属性是自定义对象的列表

e、 g.类
A
对象中的
X3
属性是类
B
对象的
列表

class A 
{
    public int X1 { get; set; }

    public string X2 { get; set; }

    public List<B> X3 { get; set; }
}

迭代类
A
对象的属性以获得属性的非空值和列表中任何嵌套对象的属性的最佳方法是什么?

您可以使用.NET的内置反射API迭代类型的属性。快速和肮脏的例子:

public static void Main(string[] args)
{
  var a = new A()
  {
    X1 = 1,
    X2 = "2",
    X3 = new List<B> { new B { Y1 = 5, Y2 = 7 } }
  };

  PrintProperties(a);
}

private static void PrintProperties(object obj)
{
  foreach (var prop in obj.GetType().GetProperties())
  {
    var propertyType = prop.PropertyType;
    var value = prop.GetValue(obj);

    Console.WriteLine("{0} = {1} [{2}]", prop.Name, value, propertyType);

    if (typeof(IList).IsAssignableFrom(propertyType))
    {
      var list = (IList)value;

      foreach (var entry in list)
      {
        PrintProperties(entry);
      }
    }
    else
    {
      if (prop.PropertyType.Namespace != "System")
      {
        PrintProperties(value);
      }
    }
  }
}
publicstaticvoidmain(字符串[]args)
{
var a=新的a()
{
X1=1,
X2=“2”,
X3=新列表{new B{Y1=5,Y2=7}
};
打印属性(a);
}
私有静态void打印属性(对象obj)
{
foreach(obj.GetType().GetProperties()中的var prop)
{
var propertyType=prop.propertyType;
var值=道具获取值(obj);
WriteLine(“{0}={1}[{2}]”,prop.Name,value,propertyType);
if(typeof(IList).IsAssignableFrom(propertyType))
{
变量列表=(IList)值;
foreach(列表中的var条目)
{
打印属性(条目);
}
}
其他的
{
if(prop.PropertyType.Namespace!=“系统”)
{
打印属性(值);
}
}
}
}

正如您已经被告知的,当您的类型在编译时已知时,不应该使用这样的代码

你确定你不想要字典吗?通过反射可以迭代属性,但在许多情况下,如果您发现自己将类视为属性包,则会产生代码气味。这应该是
public List X3{get;set;}
?这不可能编译。。你没有在这里得到一个
类型预期的
错误`public List X3{get;set;}`再加上Wai的好评论,OP的代码不会编译这工作起来很有魅力..谢谢Gene
public static void Main(string[] args)
{
  var a = new A()
  {
    X1 = 1,
    X2 = "2",
    X3 = new List<B> { new B { Y1 = 5, Y2 = 7 } }
  };

  PrintProperties(a);
}

private static void PrintProperties(object obj)
{
  foreach (var prop in obj.GetType().GetProperties())
  {
    var propertyType = prop.PropertyType;
    var value = prop.GetValue(obj);

    Console.WriteLine("{0} = {1} [{2}]", prop.Name, value, propertyType);

    if (typeof(IList).IsAssignableFrom(propertyType))
    {
      var list = (IList)value;

      foreach (var entry in list)
      {
        PrintProperties(entry);
      }
    }
    else
    {
      if (prop.PropertyType.Namespace != "System")
      {
        PrintProperties(value);
      }
    }
  }
}