0

我有一个现有的窗口窗体应用程序。该应用程序有一个属性网格。属性的值由用户在运行时设置。我想做的是从代码中确定任何给定属性的当前值。我取得了部分成功。我能够获取类别以及属性名称信息。我很难获得用户设置的属性的当前值,以及其他两个相关问题。

我正在使用的代码如下:

 // 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;
 }

我的问题是:

  1. 如何获取属性的值?例如,我有一个属性纵横比。它的显示名称是 Aspect Ratio,它的名称是 aspectRatio。它的值为 5。如何在运行时通过代码检索它?

  2. 我如何订购这些属性。我尝试使用上述方法对属性进行排序,但排序失败。我不确定如何最好地进行。

任何建议将不胜感激。谢谢你。

4

1 回答 1

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; }
    }

}
于 2012-12-03T23:05:26.970 回答