C# 在对象内的对象之间循环
我有一个类C# 在对象内的对象之间循环,c#,C#,我有一个类报警,它包含不同型号的多个列表。每个模型都是不同的,它们由多个字符串属性组成。我的目标是为每个模型创建一个CSV文件,但不需要对模型中的每个字符串属性进行harcoding public class Alarms { public List<SectorA> SectorA { get; set; } = new List<SectorA>(); public List<SectorB> SectorB { get; set; } = n
报警
,它包含不同型号的多个列表
。每个模型都是不同的,它们由多个字符串属性组成。我的目标是为每个模型创建一个CSV文件,但不需要对模型中的每个字符串属性进行harcoding
public class Alarms
{
public List<SectorA> SectorA { get; set; } = new List<SectorA>();
public List<SectorB> SectorB { get; set; } = new List<SectorB>();
public List<SectorC> SectorC { get; set; } = new List<SectorC>();
}
我的问题是如何循环我的报警
类,以在一个循环中获取其中的每个列表
编辑:
一个SectorX
类的示例
public class SectorA
{
public string Id { get; set; }
public string Group { get; set; }
public string Comment { get; set; }
...
}
编辑#2
这是一个函数,我必须循环通过一个类来获得它的属性
public void WriteCsv<T>(string csvOutputPath, List<object> sectors)
{
using (var w = new StreamWriter(csvOutputPath))
{
foreach (var lines in sectors)
{
string line = "";
foreach (PropertyInfo prop in lines.GetType().GetProperties())
{
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
if (type == typeof(string))
{
line += $"{prop.GetValue(lines, null)};";
}
}
line = line.Remove(line.Length - 1);
w.WriteLine(line);
w.Flush();
}
}
}
public void WriteCsv(字符串csvoutput路径,列表扇区)
{
使用(var w=新StreamWriter(csvOutputPath))
{
foreach(部门中的var行)
{
字符串行=”;
foreach(行中的PropertyInfo prop.GetType().GetProperties())
{
var type=Nullable.getUnderlineType(prop.PropertyType)??prop.PropertyType;
if(type==typeof(string))
{
line+=$“{prop.GetValue(line,null)};”;
}
}
line=line.Remove(line.Length-1);
w、 写线(行);
w、 冲洗();
}
}
}
不是最好的方法,但是
private void Test(object item)
{
var props = item.GetType().GetProperties();
foreach (var prop in props)
{
object value = prop.GetValue(item);
if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(List<>) && value != null)
// if (prop.PropertyType.IsInterface && value != null)
{
foreach (var iItem in (System.Collections.IEnumerable)value)
{
Console.WriteLine(iItem.ToString());
}
}
}
}
专用无效测试(目标项)
{
var props=item.GetType().GetProperties();
foreach(道具中的var道具)
{
对象值=道具获取值(项目);
if(prop.PropertyType.IsGenericType&&prop.PropertyType.GetGenericTypeDefinition()==typeof(List)&&value!=null)
//if(prop.PropertyType.IsInterface&&value!=null)
{
foreach(变量iItem in(System.Collections.IEnumerable)值)
{
Console.WriteLine(iItem.ToString());
}
}
}
}
也许GetFields()方法可以在这里提供帮助
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args)
{
Type objType = typeof(Alarms);
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
FieldInfo[] fieldInfos = objType.GetFields(bindingFlags);
for (int i = 0; i < fieldInfos.Length; i++)
Console.WriteLine(" {0}", fieldInfos[i]);
Console.ReadKey();
}
}
}
public class Alarms
{
public List<SectorA> SectorA { get; set; } = new List<SectorA>();
}
public class SectorA
{
public string Id { get; set; }
public string Group { get; set; }
public string Comment { get; set; }
}
使用系统;
使用System.Collections.Generic;
运用系统反思;
名称空间控制台EAPP1
{
公共课程
{
静态void Main(字符串[]参数)
{
类型objType=类型(报警);
BindingFlags BindingFlags=BindingFlags.Public|
BindingFlags.NonPublic|
BindingFlags.Instance|
BindingFlags.Static;
FieldInfo[]fieldInfos=objType.GetFields(bindingFlags);
for(int i=0;i
您打算如何使用所有这些列表?我想循环浏览每一行,并为它们创建一个单独的CSV文件。例如,在文件夹Alarm\u Name
中,我将有3个csv,sector1.csv
,sector2.csv
,etc@RegularNormalDayGuy您的属性是List
,而不是SectorA
…没错,但是我的代码没有进入if块,我知道value
中至少有一个值可能会起作用,但使用IsClass
有什么害处?不,它不起作用。我的函数WriteCsv
的签名很难处理,我无法将对象
转换为系统.Collections.Generic.List
。我不能改变它的对象,因为那样我就不能遍历它的属性了
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args)
{
Type objType = typeof(Alarms);
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
FieldInfo[] fieldInfos = objType.GetFields(bindingFlags);
for (int i = 0; i < fieldInfos.Length; i++)
Console.WriteLine(" {0}", fieldInfos[i]);
Console.ReadKey();
}
}
}
public class Alarms
{
public List<SectorA> SectorA { get; set; } = new List<SectorA>();
}
public class SectorA
{
public string Id { get; set; }
public string Group { get; set; }
public string Comment { get; set; }
}