如何使用反射来访问类型中的 IEnumerable 成员?

发布于 2025-01-09 03:53:25 字数 2486 浏览 0 评论 0原文

如何访问类型中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文