Не можете вывести тип в C #, должны установить его явно? - PullRequest
3 голосов
/ 28 ноября 2010

это мой код: код работал с первыми + вторыми параметрами, когда я добавил третий параметр, он больше не компилируется, что мне нужно изменить, чтобы он работал?

 /// <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 Ответ

7 голосов
/ 28 ноября 2010

Проблема заключается в введенной вами неоднозначности, поскольку как 2-й, так и 3-й параметры могут выводить TProperty. Возможно, вы захотите попробовать ввести третий параметр универсального типа, чтобы у вас были TDisplayProperty и TSortProperty, что должно быть хорошо для вашего варианта использования.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...