首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索嵌套对象的PropertyDescriptor

检索嵌套对象的PropertyDescriptor
EN

Stack Overflow用户
提问于 2012-02-02 16:08:07
回答 1查看 1.2K关注 0票数 1

我有以下代码:

代码语言:javascript
复制
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项目?我这样做对吗?

谢谢,Bill N

EN

回答 1

Stack Overflow用户

发布于 2012-04-26 23:02:49

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

这回答了你的直接问题,但我不清楚你的意图。也许如果你能重新表达你的问题,并清楚地说明你试图用这段代码完成什么,你可能会得到一个更好的答案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9108960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档