gpt4 book ai didi

c# - 使用反射从复杂类中获取值

转载 作者:数据小太阳 更新时间:2023-10-29 02:06:28 25 4
gpt4 key购买 nike

我有一个类,它是从一个 xml 字符串创建和填充的,为了示例目的我已经简化了它:

[XmlRoot("Person")]
public sealed class Person
{
[XmlElement("Name")]
public string Name { get; set; }

[XmlElement("Location")]
public string Location { get; set; }

[XmlElement("Emails", Type = typeof(PersonEmails)]
public PersonEmails Emails { get; set; }
}

public class PersonEmails
{
[XmlElement("Email", Type = typeof(PersonEmail))]
public PersonEmail[] Emails { get; set; }
}

public class PersonEmail
{
[XmlAttribute("Type")]
public string Type { get; set; }

[XmlText]
public string Value { get; set; }
}

为了提取信息,我试图将它们加载到另一个类中,这很简单:

public class TransferObject
{
public string Name { get; set; }

public ObjectField[] Fields { get; set; }
}

public class ObjectField
{
public string Name { get; set; }
public string Value { get; set; }
}

我只是从另一个对象填充“字段”,它只是 (Name = "Location", Value = "London"),但对于电子邮件,(Name = "Email"+Type, Value = jeff @这里.com)

目前我可以填充所有其他字段,但我受困于电子邮件,并且知道如何深入挖掘以便能够使用(或不使用)反射来获取我需要的信息。目前我正在使用:

Person person = Person.FromXmlString(xmlString);
List<ObjectField> fields = new List<ObjectField>();
foreach (PropertyInfo pinfo in person.getType().GetProperties()
{
fields.Add(new ObjectField { Name = pinfo.Name, Value = pinfo.getValue(person, null).ToString();
}

我如何扩展上述内容以将我所有的电子邮件添加到列表中?

最佳答案

您正在尝试将复杂值类型强制转换为字符串值,因此您丢失了数据。而是使用以下代码:

class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "Person One";
person.Location = "India";
person.Emails = new PersonEmails();
person.Phones = new PersonPhones();
person.Emails.Emails = new PersonEmail[] { new PersonEmail() { Type = "Official", Value = "xyz@official.com" }, new PersonEmail() { Type = "Personal", Value = "xyz@personal.com" } };
person.Phones.Phones = new PersonPhone[] { new PersonPhone() { Type = "Official", Value = "789-456-1230" }, new PersonPhone() { Type = "Personal", Value = "123-456-7890" } };

List<ObjectField> fields = new List<ObjectField>();

fields = GetPropertyValues(person);

}

static List<ObjectField> GetPropertyValues(object obj)
{
List<ObjectField> propList = new List<ObjectField>();

foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
{
var value = pinfo.GetValue(obj, null);

if (pinfo.PropertyType.IsArray)
{
var arr = value as object[];
for (var i = 0; i < arr.Length; i++)
{
if (arr[i].GetType().IsPrimitive)
{
propList.Add(new ObjectField() { Name = pinfo.Name + i.ToString(), Value = arr[i].ToString() });
}
else
{
var lst = GetPropertyValues(arr[i]);
if (lst != null && lst.Count > 0)
propList.AddRange(lst);
}
}
}
else
{
if (pinfo.PropertyType.IsPrimitive || value.GetType() == typeof(string))
{
propList.Add(new ObjectField() { Name = pinfo.Name, Value = value.ToString() });
}
else
{
var lst = GetPropertyValues(value);
if (lst != null && lst.Count > 0)
propList.AddRange(lst);
}
}
}
return propList;
}
}

关于c# - 使用反射从复杂类中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21091452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com