C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法推断C#中的类型,必须明确设置吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码代码使用第一个第二个参数,当我添加第三个参数时它不再编译,我需要更改以使其工作?

/// <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.
@H_197_24@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。