代码之家  ›  专栏  ›  技术社区  ›  Elisabeth

无法推断C中的类型,必须显式设置它?

  •  3
  • Elisabeth  · 技术社区  · 15 年前

    这是我的代码:代码使用第一个+第二个参数,当我添加第三个参数时,它不再编译,我需要做些什么才能使它工作?

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

    它不再工作了?

    1 回复  |  直到 15 年前
        1
  •  7
  •   Lucero    15 年前

    问题在于你引入的模糊性,因为第二个和第三个参数都可以推断 TProperty . 您可能希望尝试引入第三个泛型类型参数,以便 TDisplayProperty TSortProperty 这对你的用例来说应该没问题。