开发者

Can not infer the type in C# , have to set it explicitly?

开发者 https://www.devze.com 2023-01-27 01:08 出处:网络
this is my code: The code worked with the first+second parameters, when I added the 3rd paramter it does not compile anymore, what do I have to change to make it working?

this is my code: The code worked with the first+second parameters, when I added the 3rd paramter it does not compile anymore, what do I have to change to make it working?

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

this worked: ElementUserControl.BindElements(customers, c => c.CustomerId);

but when I added the 3rd paramter:

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

It did not work anymore?


The problem lies in the ambiguity which you introduce, since both the 2nd and the 3rd parameter can infer TProperty. You might want to try and introduce a third generic type parameter so that you have TDisplayProperty and TSortProperty which should be okay for your use case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号