考虑我们有这样的模型:
class Person{
public string Name{get; set;}
public List<Contact> ContactInfo{get; set;}
}
和联系方式是这样的:
class Contact{
public string Landline{get; set;}
public string Mobile{get; set;}
}
我想ContactInfo(List<Contact>)
使用 C# 中的反射访问并因此获取 Landline 和 Mobile 的值。
private static string GetReflectedLandlineValue(object person)
{
IEnumerable<object> contactList = (IEnumerable<object>)person.GetType().GetProperty("ContactInfo")?.GetValue(person);
return (string) contactList.First().GetType().GetProperty("Landline")?.GetValue(contactList.First());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句