2011-10-04 6 views
2

Observable 동적 개체 컬렉션을 정렬하려고합니다. IComparer 구현을 시도했지만 동적 인 인터페이스를 구현할 수 없다고 알려줍니다. 나는 지금 붙어있다. 이 아이디어를 어떻게 구체화 할 수 있을까요? OrderBy, ObservableCollection <dynamic>, ICompare

나는 IComparer<object>으로, 컴파일시에, 동일이

list.OrderByDescending(x => x, new DynamicSerializableComparer()); 

을 시도하고 IComparer

public class DynamicSerializableComparer : IComparer<dynamic> 
     { 
      string _property; 

      public DynamicSerializableComparer(string property) 
      { 
       _property = property; 
      } 

      public int Compare(dynamic stringA, dynamic stringB) 
      { 
       string valueA = stringA.GetType().GetProperty(_property).GetValue(); 
       string valueB = stringB.GetType().GetProperty(_property).GetValue(); 

       return String.Compare(valueA, valueB); 
      } 

     } 
+0

그냥 객체 (ExpandoObject' 또는 IronPython의에서 오는 약간의 개체'처럼) 실제로 동적하면이 구현은 작동하지 않습니다 –

답변

0

IComparer<dynamic>입니다. That's why you can't implement it.

대신 IComparer<object>을 구현하고 동적으로 전송 해보세요.

public class DynamicSerializableComparer : IComparer<object> 
{ 
    string _property; 

    public DynamicSerializableComparer(string property) 
    { 
     _property = property; 
    } 

    public int Compare(object stringA, object stringB) 
    { 
     string valueA = stringA.GetType().GetProperty(_property).GetValue(); 
     string valueB = stringB.GetType().GetProperty(_property).GetValue(); 

     return String.Compare(valueA, valueB); 
    } 

} 
+1

IComparer이 아닌 일반 버전을 구현합니다. – svick

+0

@ 스빅 : 왜죠? –

+0

'Type.GetProperty()'는 동적 속성을 제공하지 않기 때문에. 'Type'은'dynamic' 또는 DLR에 대해 아무것도 모릅니다 (제 생각에는). – svick

관련 문제