使用反射遍历嵌套在对象中的对象列表
Iterate through Object List nested within Object using Reflection
我正在尝试使用反射生成基于表单输入的电子邮件,以便以更快、希望更好的方式生成电子邮件。
迭代 Company 对象中的嵌套列表,以便 _body
是 Company 中所有属性的长输出列表及其嵌套对象列表(Asn
和 Contact
)。
当该方法接受一个 Company 对象时,它会遍历 Company 中的每个 属性 并检查它是什么类型。我即将撤消检查 typeof List<> 的工作,我不知道为什么它没有遍历集合。
我整个上午都在研究代码,但似乎一无所获。
我哪里错了?
基本模型结构:
public class Company {
public string name {get;set;}
public List<Asn> asns {get;set;}
public List<Contact> contacts {get;set;}
}
public class Asn {
// string/int/bool properties
}
public class Company {
// string/int/bool properties
}
问题方法代码:
public static void SendEmail(Company cm)
{
string _body = "";
string _subject = "ASN Form Request";
Type type = cm.GetType();
Type type2 = cm.asns.GetType();
Type type3 = cm.contacts.GetType();
PropertyInfo[] companyProperties = type.GetProperties();
PropertyInfo[] asnProperties = type2.GetProperties();
PropertyInfo[] contactProperties = type3.GetProperties();
foreach(PropertyInfo property in companyProperties)
{
if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int) || property.PropertyType == typeof(bool))
_body += property.Name + " = " + property.GetValue(cm, null) + Environment.NewLine;
if (property.PropertyType == typeof(List<>)) // not running through the model properties
foreach(PropertyInfo asnproperty in asnProperties)
{
_body += asnproperty.Name + " = " + asnproperty.GetValue(cm, null) + Environment.NewLine;
}
if(property.PropertyType == typeof(List<>)) // not running through the model properties
foreach(PropertyInfo contactproperty in contactProperties)
{
_body += contactproperty.Name + " = " + contactproperty.GetValue(cm, null) + Environment.NewLine;
}
}
}
如果我理解正确的话,您想要在列表的内部属性中输出公司和任何属性。
public static void SendEmail(Company cm)
{
string _body = "";
string _subject = "ASN Form Request";
_body = ReflectObject(cm, _body)
}
public static string ReflectObject(object obj, string body)
{
var type = obj.GetType();
var properties = type.GetProperties();
foreach(PropertyInfo property in properties)
{
if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int) || property.PropertyType == typeof(bool))
body += property.Name + " = " + property.GetValue(cm, null) + Environment.NewLine;
if (property.PropertyType == typeof(List<>))
{
var list = property.GetValue(obj, null)
foreach(var item in list)
{
ReflectObject(item, body);
}
}
}
return body;
}
我正在尝试使用反射生成基于表单输入的电子邮件,以便以更快、希望更好的方式生成电子邮件。
迭代 Company 对象中的嵌套列表,以便 _body
是 Company 中所有属性的长输出列表及其嵌套对象列表(Asn
和 Contact
)。
当该方法接受一个 Company 对象时,它会遍历 Company 中的每个 属性 并检查它是什么类型。我即将撤消检查 typeof List<> 的工作,我不知道为什么它没有遍历集合。
我整个上午都在研究代码,但似乎一无所获。
我哪里错了?
基本模型结构:
public class Company {
public string name {get;set;}
public List<Asn> asns {get;set;}
public List<Contact> contacts {get;set;}
}
public class Asn {
// string/int/bool properties
}
public class Company {
// string/int/bool properties
}
问题方法代码:
public static void SendEmail(Company cm)
{
string _body = "";
string _subject = "ASN Form Request";
Type type = cm.GetType();
Type type2 = cm.asns.GetType();
Type type3 = cm.contacts.GetType();
PropertyInfo[] companyProperties = type.GetProperties();
PropertyInfo[] asnProperties = type2.GetProperties();
PropertyInfo[] contactProperties = type3.GetProperties();
foreach(PropertyInfo property in companyProperties)
{
if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int) || property.PropertyType == typeof(bool))
_body += property.Name + " = " + property.GetValue(cm, null) + Environment.NewLine;
if (property.PropertyType == typeof(List<>)) // not running through the model properties
foreach(PropertyInfo asnproperty in asnProperties)
{
_body += asnproperty.Name + " = " + asnproperty.GetValue(cm, null) + Environment.NewLine;
}
if(property.PropertyType == typeof(List<>)) // not running through the model properties
foreach(PropertyInfo contactproperty in contactProperties)
{
_body += contactproperty.Name + " = " + contactproperty.GetValue(cm, null) + Environment.NewLine;
}
}
}
如果我理解正确的话,您想要在列表的内部属性中输出公司和任何属性。
public static void SendEmail(Company cm)
{
string _body = "";
string _subject = "ASN Form Request";
_body = ReflectObject(cm, _body)
}
public static string ReflectObject(object obj, string body)
{
var type = obj.GetType();
var properties = type.GetProperties();
foreach(PropertyInfo property in properties)
{
if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int) || property.PropertyType == typeof(bool))
body += property.Name + " = " + property.GetValue(cm, null) + Environment.NewLine;
if (property.PropertyType == typeof(List<>))
{
var list = property.GetValue(obj, null)
foreach(var item in list)
{
ReflectObject(item, body);
}
}
}
return body;
}