检索嵌套对象的 PropertyDescriptor

发布于 2025-01-01 10:39:14 字数 2821 浏览 1 评论 0原文

我有以下代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        object o;
        Person p = new Person { FirstName = "John", Surname = "Henry" };
        Citizen c = new Citizen { Country = "Canada", ResidentName = p };
        SportsFan sf = new SportsFan { Sport = "Hockey", Fan = c };

        Discoverer<SportsFan>.SimpleExample("Sport", "Hockey",out o);
        Discoverer<SportsFan>.NestedProperyExample("Fan.Citizen.FirstName", "John",out o);
    }

    private class Person
    {
        public string FirstName
        {
            get; set;
        }

        public string Surname
        {
            get; set;
        }
    }

    private class Citizen
    {
        public Person ResidentName
        {
            get; set;
        }

        public string Country
        {
            get; set;
        }
    }

    private class SportsFan
    {
        public string Sport
        {
            get; set;
        }

        public Citizen Fan
        {
            get; set;
        }
    }

    private class Discoverer<T>
    {
        public static void SimpleExample(string propName, string objResultToString,out Object obj)
        {
            PropertyDescriptor propDesc;
            propDesc = TypeDescriptor.GetProperties(typeof(T))[propName];               

            TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
            obj = converter.ConvertFromString(objResultToString);                   
        }

        public static void NestedProperyExample(string propName, string objResultToString, out Object obj)
        {
            PropertyDescriptor propDesc = null;
            obj = null;
            string[] nestedProperties = propName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            propDesc = TypeDescriptor.GetProperties("Form1." + nestedProperties[0])[nestedProperties[1]];
            for (int i = 1; i < nestedProperties.Length - 1; i++)
            {
                if (propDesc != null)
                    propDesc = TypeDescriptor.GetProperties(propDesc.GetType())[nestedProperties[i + 1]];
            }

            if (propDesc != null)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
                obj = converter.ConvertFromString(objResultToString);
            }

        }
    }


}

该代码适用于 simpleExample。在NestedPropertyExample 上,对PropDesc 的第一个赋值返回null。当我检查 TypeDescriptor.GetProperties("Form1." + NestedProperties[0]) 时,它返回一项的 PropertyDescriptorCollection ,即长度。

为什么我没有返回更多 PropertyDesriptor 项目?我以正确的方式处理这个问题吗?

谢谢,比尔·N

I have the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        object o;
        Person p = new Person { FirstName = "John", Surname = "Henry" };
        Citizen c = new Citizen { Country = "Canada", ResidentName = p };
        SportsFan sf = new SportsFan { Sport = "Hockey", Fan = c };

        Discoverer<SportsFan>.SimpleExample("Sport", "Hockey",out o);
        Discoverer<SportsFan>.NestedProperyExample("Fan.Citizen.FirstName", "John",out o);
    }

    private class Person
    {
        public string FirstName
        {
            get; set;
        }

        public string Surname
        {
            get; set;
        }
    }

    private class Citizen
    {
        public Person ResidentName
        {
            get; set;
        }

        public string Country
        {
            get; set;
        }
    }

    private class SportsFan
    {
        public string Sport
        {
            get; set;
        }

        public Citizen Fan
        {
            get; set;
        }
    }

    private class Discoverer<T>
    {
        public static void SimpleExample(string propName, string objResultToString,out Object obj)
        {
            PropertyDescriptor propDesc;
            propDesc = TypeDescriptor.GetProperties(typeof(T))[propName];               

            TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
            obj = converter.ConvertFromString(objResultToString);                   
        }

        public static void NestedProperyExample(string propName, string objResultToString, out Object obj)
        {
            PropertyDescriptor propDesc = null;
            obj = null;
            string[] nestedProperties = propName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            propDesc = TypeDescriptor.GetProperties("Form1." + nestedProperties[0])[nestedProperties[1]];
            for (int i = 1; i < nestedProperties.Length - 1; i++)
            {
                if (propDesc != null)
                    propDesc = TypeDescriptor.GetProperties(propDesc.GetType())[nestedProperties[i + 1]];
            }

            if (propDesc != null)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
                obj = converter.ConvertFromString(objResultToString);
            }

        }
    }


}

The code works for the simpleExample. On the NestedPropertyExample, the first assignment to PropDesc returns null. When I check TypeDescriptor.GetProperties("Form1." + nestedProperties[0]), it returns a PropertyDescriptorCollection of one item and that is Length.

Why am I not returning more PropertyDesriptor items? Am I going about this the correct way?

Thanks, Bill N

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2025-01-08 10:39:14

NestedProperyExample 方法拼写有点错误,但不要介意 - 这不是问题(:实际上,问题可能是 NestedProperyExample 方法调用 TypeDescriptor.GetProperties(Object) 重载,向其传递一些字符串 ("Form1." +nestedProperties[0]) 根据文档。 (MSDN),它的行为非常像TypeDescriptor.GetProperties(typeof(string))string 只有一个简单的属性,即它的Length,——这就是原因。 TypeDescriptor.GetProperties 不再返回任何 PropertyDescriptor 项目。

这回答了您的直接问题,但我不清楚您的意图,如果您可以重新表述您的问题并明确说明的话。如果你想用这段代码来完成什么,你可能会得到更好的答案。

The NestedProperyExample method is misspelled a bit, but don't mind -- it's not the problem (: Actually, the problem might be, that NestedProperyExample method calls the TypeDescriptor.GetProperties(Object) overload, passing it some string ("Form1." + nestedProperties[0]). As per docs (MSDN), it acts very much like just TypeDescriptor.GetProperties(typeof(string)). string's got only one simple property, its Length, -- and that's why TypeDescriptor.GetProperties does not return any more PropertyDescriptor items.

This answers your direct question, but your intentions are unclear to me. May be if you could rephrase your question and state clear what you're trying to accomplish with this code, you'll likely get a better answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文