无法推断C#中的类型,必须明确设置吗?
发布时间:2020-12-16 01:39:32 所属栏目:百科 来源:网络整理
导读:这是我的代码:代码使用第一个第二个参数,当我添加第三个参数时它不再编译,我需要更改以使其工作? /// summary /// Binds all dataObjects e.g. IPersonList,IDepartmentList,ITopicList... and creates a visual list of elements to display in the Eleme
这是我的代码:代码使用第一个第二个参数,当我添加第三个参数时它不再编译,我需要更改以使其工作?
/// <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.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- git ssh 生成
- AIX下 Oracle Database 11.2使用FCoE时的认证情况.
- ruby-on-rails – 在nil对象上使用strftime – 如何防止错误
- 我可以在C#中找到BigInteger的位数吗?
- ruby-on-rails – Ruby on rails – 软件包安装过程中出错
- 对PostgreSQL 的 hash join 的原理的学习
- 遍历聚合对象中的元素——迭代器模式(一)
- c# – Roslyn – 在SyntaxNode上调用ToString而不保留优先级
- oracle – DBMS_OUTPUT.PUT_LINE()的PL / SQL别名?
- Database / NoSQL – 检索以下数据的最低延迟方式