Can't a type be deduced in C #, set it explicitly?

this is my code: the code worked with the first + second parameters, when I added the third parameter, which it no longer compiles, what do I need to change to make it work?

 /// <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 third parameter:

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

Doesn't it work anymore?

+3
source share
1 answer

The problem is the ambiguity that you enter, since both the second and third parameter can output TProperty. You might want to try introducing a third type of type type so that you have TDisplayPropertyand TSortPropertythat should be in order for your use case.

+7
source

Source: https://habr.com/ru/post/1777237/


All Articles