如何使用反射来访问类型中的 IEnumerable 成员?
如何访问类型中的 IEnumerable 成员。
在这种情况下,Likes 属性永远不会在 References 类的 Retrieve 方法中公开。
[Test]
public void Test1()
{
// Setup
var person = new Person("some_id", "John", "Doe", new List<string>{ "Dogs" });
var persons = new List<Person> () { person };
var valuePredicates = new List<Predicate<string>>() { v => v.EndsWith("Id") };
// Test
var references = References.Retrieve(person, new List<KeyValuePair<string,string>>(), valuePredicates);
}
}
public static class References
{
public static IEnumerable<KeyValuePair<string, string>> Retrieve(object obj, List<KeyValuePair<string, string>> entries, IEnumerable<Predicate<string>> predicates)
{
var type = obj.GetType();
if (type.IsPrimitive || type == typeof(string)) return entries;
var structure = type.Name;
foreach (PropertyInfo property in type.GetProperties(
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Instance).Where(
x => x.CanRead &&
x.GetGetMethod(false) != null &&
x.GetIndexParameters().Length == 0))
{
var getMethod = property.GetGetMethod(true);
if (getMethod != null)
{
foreach (var isPropertyOfName in predicates)
{
if (isPropertyOfName(property.Name))
{
var identifier = $"{structure}::{property.Name}";
var textValue = property.GetValue(obj) as string;
entries.Add(new KeyValuePair<string, string>(identifier, textValue));
}
}
Retrieve(getMethod.Invoke(obj, null), entries, predicates);
}
}
return entries;
}
附录:
public class Person
{
public Person(string id, string firstName, string lastName, IEnumerable<string> likes) =>
(Id, FirstName, LastName, Likes) = (id, firstName, lastName, likes);
public string Id { get; }
public string FirstName { get; }
public string LastName { get; }
public IEnumerable<string> Likes { get; }
}
How do I access IEnumerable members within a type.
In this case, the Likes property is never exposed in the Retrieve method of the References class.
[Test]
public void Test1()
{
// Setup
var person = new Person("some_id", "John", "Doe", new List<string>{ "Dogs" });
var persons = new List<Person> () { person };
var valuePredicates = new List<Predicate<string>>() { v => v.EndsWith("Id") };
// Test
var references = References.Retrieve(person, new List<KeyValuePair<string,string>>(), valuePredicates);
}
}
public static class References
{
public static IEnumerable<KeyValuePair<string, string>> Retrieve(object obj, List<KeyValuePair<string, string>> entries, IEnumerable<Predicate<string>> predicates)
{
var type = obj.GetType();
if (type.IsPrimitive || type == typeof(string)) return entries;
var structure = type.Name;
foreach (PropertyInfo property in type.GetProperties(
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Instance).Where(
x => x.CanRead &&
x.GetGetMethod(false) != null &&
x.GetIndexParameters().Length == 0))
{
var getMethod = property.GetGetMethod(true);
if (getMethod != null)
{
foreach (var isPropertyOfName in predicates)
{
if (isPropertyOfName(property.Name))
{
var identifier = quot;{structure}::{property.Name}";
var textValue = property.GetValue(obj) as string;
entries.Add(new KeyValuePair<string, string>(identifier, textValue));
}
}
Retrieve(getMethod.Invoke(obj, null), entries, predicates);
}
}
return entries;
}
Appendix:
public class Person
{
public Person(string id, string firstName, string lastName, IEnumerable<string> likes) =>
(Id, FirstName, LastName, Likes) = (id, firstName, lastName, likes);
public string Id { get; }
public string FirstName { get; }
public string LastName { get; }
public IEnumerable<string> Likes { get; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论