2012-12-03 2 views
0

저는 기존 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; 
} 

내 질문

은 다음과 같습니다 :

내가 사용하고 코드는 다음과 같습니다

  1. 어떻게 속성 값을받을 수 있나요? 예를 들어 속성 ​​가로 세로 비율이 있습니다. 표시 이름은 Aspect Ratio이고 이름은 aspectRatio입니다. 이 값은 5입니다. 런타임시 코드를 통해이를 어떻게 retieve 할 수 있습니까?

  2. 어떻게 주문할 수 있습니까? 위의 방법을 사용하여 속성을 주문하려고했지만 주문이 실패했습니다. 계속 진행하는 것이 최선인지 모르겠습니다.

모든 의견을 크게 기뻐할 것입니다. 고맙습니다.

+0

기본 반사에서이 새로운 fangled 접근 방식에 빠져있을 수도 있지만 코드에 인스턴스가 없으므로 어떻게 될 수 있습니까? 가치? 당신은 객체의 PropertyDescriptionCollection을 원하지만 객체의 유형은 아닙니다. –

답변

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

} 
+0

"GetPropertyOwner"기능의 makr 사용으로 편집되었습니다. –

+0

고맙습니다. 이것은 거의 모든 속성에서 잘 작동했습니다. 그러나 내 속성 중 하나는 데이터 시리즈이며이 경우에는 작동하지 않습니다. 그것은 나에게 사용자가 입력 한 시리즈의 이름을주지 않았다. 단순히 DataSeriesHelper 문자열을 반환했습니다. 어떤 제안? 또한, 모든 제안은 사용자 정의 속성을 주문? 다시 한번 대단히 감사합니다. – PBrenek

+0

GetValue는 객체의 값을 반환합니다. 모든 객체가 예쁜 .ToString()을 가지고있는 것은 아닙니다. 이러한 객체가 Custom 객체 인 경우 .ToString()을 재정 의하여 원하는 것을 반환 할 수 있습니다. 또는 도우미 메서드를 추가하여 목록, 배열 등의 속성에 좋은 문자열을 만들 수 있습니다. –