[英]Determine Property Value
I have an existing Window Forms application. 我有一个现有的Window Forms应用程序。 The application has a property grid.
该应用程序具有属性网格。 The values of the properties are set by the user at runtime.
这些属性的值由用户在运行时设置。 What I would like to do is determine from code the current value of any given property.
我想做的是根据代码确定任何给定属性的当前值。 I have had partial success.
我取得了部分成功。 I am able to get the Category as well as the property name information.
我可以获取类别以及属性名称信息。 I am having difficulty getting the current value of the properties as set by the user, as well as two other related questions.
我很难获得用户设置的属性的当前值以及其他两个相关问题。
The code I am using is as follows: 我使用的代码如下:
// ICustomTypeDescriptor Interface Implementation
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(GetType());
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(GetType());
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(GetType());
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(GetType());
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(GetType());
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(GetType());
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(GetType(), editorBaseType);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(GetType(), attributes);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(GetType());
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
// ... This returns a list of properties.
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(GetType(), attributes);
PropertyDescriptor[] arr = new PropertyDescriptor[pdc.Count];
pdc.CopyTo(arr, 0);
PropertyDescriptorCollection propertyCollection = new PropertyDescriptorCollection(arr);
ModifyProperties(propertyCollection); // modifies which properties are visible
// temporary code to get the program to print out the properties
foreach (PropertyDescriptor pd in propertyCollection)
{
Print("input category = "+pd.Category);
Print("input display name = "+pd.DisplayName);
Print("input name = "+pd.Name);
// Print("input value = "+pd.GetValue(Input).ToString()); <--- Does NOT work
}
return propertyCollection;
}
public PropertyDescriptorCollection GetProperties()
{
return TypeDescriptor.GetProperties(GetType());
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
My questions are: 我的问题是:
How can I get the values of the properties? 如何获取属性的值? For example, I have a property aspect ratio.
例如,我有一个属性长宽比。 Its display name is Aspect Ratio and its name is aspectRatio.
它的显示名称是Aspect Ratio,其名称是AspectRatio。 Its value is 5. How can I retieve this via code at runtime?
它的值为5。如何在运行时通过代码来解决这个问题?
How can I order the properties. 如何订购属性。 I have tried to use the above approach to ordering the properties but the ordering failed.
我尝试使用上述方法对属性进行排序,但是排序失败。 I am not sure how best to proceed.
我不确定如何最好地进行。
Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thank you.
谢谢。
Maybe this 也许这个
foreach (PropertyDescriptor pd in propertyCollection)
{
Print("input category = "+pd.Category);
Print("input display name = "+pd.DisplayName);
Print("input name = "+pd.Name);
Print("input value = " + pd.GetValue(GetPropertyOwner(pd)));
}
If you have custom objects you want to get a nice string from you could add a helper method like below: 如果您有自定义对象,则希望从中获取漂亮的字符串,可以添加如下的帮助方法:
private string GetPropertyValue(PropertyDescriptor pd)
{
var property = GetPropertyOwner(pd);
if (property is CustomObject)
{
var dataSeries = property as CustomObject;
// This will return a string of the list contents ("One, Two, Three")
return string.Join(",", dataSeries.ListProperty.ToArray());
}
else if (property is ....)
{
return somthing else
}
return property.ToString();
}
Demo class: 示范课:
public class CustomObject
{
private List<string> _listProperty = new List<string>(new string[]{"One","Two","Three"});
public List<string> ListProperty
{
get { return _listProperty; }
set { _listProperty = value; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.