我有一个现有的Window Forms应用程序。该应用程序具有属性网格。属性的值由用户在运行时设置。我想要做的是从代码中确定任何给定属性的当前值。我取得了部分成功。我能够获得类别以及属性名称信息。我很难获得用户设置的属性的当前值,以及其他两个相关问题。
我使用的代码如下:
// 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;
}
我的问题是:
如何获取属性的值?例如,我有一个属性宽高比。它的显示名称是Aspect Ratio,它的名字是aspectRatio。它的值是5.如何在运行时通过代码来解决这个问题?
如何订购属性。我试图使用上面的方法来排序属性,但排序失败。我不确定如何最好地继续。
任何建议都将不胜感激。谢谢。
答案 0 :(得分:1)
也许这个
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)));
}
如果你有自定义对象,你想从中获得一个很好的字符串,你可以添加一个如下所示的辅助方法:
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();
}
演示课程:
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; }
}
}