检索嵌套对象的 PropertyDescriptor
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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, thatNestedProperyExample
method calls theTypeDescriptor.GetProperties(Object)
overload, passing it some string("Form1." + nestedProperties[0])
. As per docs (MSDN), it acts very much like justTypeDescriptor.GetProperties(typeof(string))
.string
's got only one simple property, itsLength
, -- and that's whyTypeDescriptor.GetProperties
does not return any morePropertyDescriptor
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.