无法推断C#中的类型,必须明确设置吗?

无法推断C#中的类型,必须明确设置吗?,第1张

概述这是我的代码:代码使用第一个第二个参数,当我添加第三个参数时它不再编译,我需要更改以使其工作? /// <summary> /// Binds all dataObjects e.g. IPersonList, IDepartmentList, ITopicList... and creates a visual list of elements to display in the 这是我的代码:代码使用第一个第二个参数,当我添加第三个参数时它不再编译,我需要更改以使其工作?

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

但是当我添加第3个参数时:

ElementUserControl.BindElements(customers,c => c.CustomerID,c => c.sortID);

它不再起作用了?

解决方法 问题在于你引入的歧义,因为第二个和第三个参数都可以推断TProperty.您可能想尝试引入第三个泛型类型参数,以便您拥有适用于您的用例的TdisplayProperty和TSortProperty. 总结

以上是内存溢出为你收集整理的无法推断C#中的类型,必须明确设置吗?全部内容,希望文章能够帮你解决无法推断C#中的类型,必须明确设置吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1216363.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存