2010-11-27 29 views
3

這是我的代碼:代碼與第一個+第二個參數一起工作,當我添加第三個參數時,它不再編譯,我必須改變它以使其工作?無法推斷C#中的類型,必須明確設置它嗎?

/// <summary> 
     /// Binds all dataObjects e.g. IPersonList, IDepartmentList, ITopicList... and creates a visual list of elements to display in the ElementTextBox 
     /// </summary> 
     /// <typeparam name="T">Type of dataObject in the dataObjects list</typeparam> 
     /// <typeparam name="TProperty">value for the Type specified by the TResult paramter</typeparam> 
     /// <param name="dataObjects">entity from database the user wants to show in the ElementTextBox</param> 
     /// <param name="selectorDisplayMember">The property like FirstName that is shown as the elements text</param> 
     /// <param name="selectorSortMember">The property like SortId that is used to pre-sort the dataObjects so the elements appear in the order before they were saved</param> 
     public void BindElements<T, TProperty>(IEnumerable<T> dataObjects, Func<T, TProperty> selectorDisplayMember, Func<T, TProperty> selectorSortMember) 
     { 
      if (dataObjects != null) 
      { 
       var sortedDataObjects = from d in dataObjects 
             orderby selectorSortMember(d) ascending 
             select d; 

       Paragraph para = new Paragraph(); 

       foreach (T item in dataObjects) 
       { 
        TProperty displayMemberValue = selectorDisplayMember(item); 
        InlineUIContainer uiContainer = ElementList.CreateElementContainer(displayMemberValue); 
        para.Inlines.Add(uiContainer); 
       } 

       FlowDocument flowDoc = new FlowDocument(para); 
       ElementList.Document = flowDoc; 
      }    
     } 

這工作:ElementUserControl.BindElements(customers, c => c.CustomerId);

但是當我加入第三放慢參數:

ElementUserControl.BindElements(customers, c => c.CustomerId, c => c.SortId); 

它沒有工作了?

+0

我想你還需要向我們展示`BindElements`的重載,它帶有兩個*參數(成功推斷出該類型的重載) – AakashM 2010-11-27 23:18:59

+0

沒有超載。具有兩個參數的方法就在上面,剛剛刪除開始時對數據對象排序的代碼。但問題似乎是參數編號3. – Elisabeth 2010-11-27 23:25:04

回答

7

問題在於你引入的模糊性,因爲第二和第三參數都可以推斷出TProperty。您可能需要嘗試引入第三個泛型類型參數,以便您有TDisplayPropertyTSortProperty這對您的用例應該沒問題。

相關問題