【问题标题】:Pass a generic delegate as Method parameter in C#在 C# 中将泛型委托作为方法参数传递
【发布时间】:2010-11-28 20:34:31
【问题描述】:

我有这个委托声明:

public delegate IEnumerable<T> SearchInputTextStrategy<T, U>(string param);

假设我确实在这里创建了新的 SearchInputTextStrategy 委托并将其命名为 MyDelegate。

这是我的方法声明:

public void BindElements<T, TDisplayProperty,TSortProperty>
(
       IEnumerable<T> dataObjects,
       Func<T, TDisplayProperty> selectorDisplayMember,
       Func<T, TSortProperty> selectorSortMember,
       string delimiter,
       // 1.) how to declare the delegate here as parameter ??
)
{
    // pass here the delegate to a private field to save it
    // 2.) how can I do that?

}

我该怎么做 1.) 和 2.) ? :-)

更新 2:

好的,这就是我到目前为止所做的:

public class SearchProvider<T>
    {
        public delegate IEnumerable<T> SearchInputTextStrategy<T>(string param);    

        public SearchInputTextStrategy<T> SearchStrategy { get; set; }

        public T TypedValue
        {
            get
            {
                return (T)Convert.ChangeType(SearchStrategy, typeof(T));
            }
        }
    }

用户控制:

 public delegate IEnumerable<T> SearchInputTextStrategy<T>(string param);

 public void BindElements<T, TDisplayProperty,TSortProperty>
        (
            IEnumerable<T> dataObjects,
            Func<T, TDisplayProperty> selectorDisplayMember,
            Func<T, TSortProperty> selectorSortMember,
            string delimiter,
            SearchInputTextStrategy<T> searchStrategy
        )
        { 
               /// assign the searchStrategy to the SearchProvider class 
            var sp = new SearchProvider<T>();
                sp.SearchStrategy = searchStrategy  // DOES NOT WORK !!!     
        }

另请阅读我在代码中的 cmets。我想要实现的是将委托传递给 searchProvider 以将其保存在某处......我在这里写的代码我理解高达 50% 所以请耐心等待,尽管我使用泛型 List 很长时间,但泛型对我来说是新的; P

更新 2:

公共部分类 MainWindow : Window { 公共委托 IEnumerable SearchInputTextStrategy(string param);

    private SearchInputTextStrategy<ICustomer> _strategy;

    public MainWindow()
    {
        InitializeComponent();            

        IEnumerable<ICustomer> customers = DataService.GetCustomers();                  

        _strategy = new SearchInputTextStrategy<ICustomer>(SearchCustomers);           


        ElementUserControl.BindElements(customers, c => c.FirstName, c => c.SortId, ";", _strategy);

namespace ElementTextBoxV2
{      

        public partial class MainWindow : Window
        {
            public delegate IEnumerable<ICustomer> SearchInputTextStrategy<ICustomer>(string param);

            private SearchInputTextStrategy<ICustomer> _strategy;

            public MainWindow()
            {
                InitializeComponent();            

                IEnumerable<ICustomer> customers = DataService.GetCustomers();                  

                _strategy = new SearchInputTextStrategy<ICustomer>(SearchCustomers);           


                ElementUserControl.BindElements(customers, c => c.FirstName, c => c.SortId, ";", _strategy);

                IEnumerable<ICustomer> selectedElements =  ElementUserControl.SelectedElements<ICustomer>();
            }

            // Just a Test-Methode to assure the delegate works
            public IEnumerable<ICustomer> SearchCustomers(string param)
            {
                IEnumerable<ICustomer> foundCustomers = new List<ICustomer>();
                return foundCustomers;
            }         
        }
    }

场景是,用户将 TextBoxUserControl 放在 MainWindow 中,他必须传递一个指向 searchMethod 的委托。我已经使用 SearchCustomers_Method 实现了这一点。问题是 C# 无法解决这个问题:

    Error   1   The best overloaded method match for 'ElementTextBoxV2.ElementsView.BindElements<ElementTextBoxV2.ICustomer,string,int>(System.Collections.Generic.IEnumerable<ElementTextBoxV2.ICustomer>, System.Func<ElementTextBoxV2.ICustomer,string>, System.Func<ElementTextBoxV2.ICustomer,int>, string, ElementTextBoxV2.Provider.SearchInputTextStrategy<ElementTextBoxV2.ICustomer>)' has some invalid arguments

Error   2   Argument 5: cannot convert from 'ElementTextBoxV2.MainWindow.SearchInputTextStrategy<ElementTextBoxV2.ICustomer>' to 'ElementTextBoxV2.Provider.SearchInputTextStrategy<ElementTextBoxV2.ICustomer>'    

你看到问题了吗?在任何情况下,用户都必须传递一个与 BindElements 方法具有相同定义的委托!

【问题讨论】:

    标签: c# parameters methods delegates


    【解决方案1】:

    奇怪的是你的SearchInputTextStrategy 有两个类型参数,但实际上只使用一个......但你只需要在参数类型中指定类型参数。例如:

    public void BindElements<T, TDisplayProperty,TSortProperty>
    (
        IEnumerable<T> dataObjects,
        Func<T, TDisplayProperty> selectorDisplayMember,
        Func<T, TSortProperty> selectorSortMember,
        string delimiter,
        SearchInputTextStrategy<T, TDisplayProperty> searchStrategy
    )
    

    我只是猜测类型参数应该是什么 - 你还没有真正说出你想要参数代表什么。

    您将无法在类中轻松地拥有正确类型的字段,因为类本身不知道所涉及的类型参数。您可能真的应该使您的类成为通用类,或者使 another 类能够适当地处理委托。没有更多信息,很难知道是哪一个。

    【讨论】:

    • 我敢打赌,委托的第二个类型参数应该是TSortProperty。但这只是像你这样的猜测
    • @Jon 和 alpha-mouse:哦,不,第二个类型参数与 TSortProperty 或 TDisplayProperty 无关,它只是传递给代理的字符串,该代理用作数据库上的搜索模式。
    • @Jon 一个人...:引用:“奇怪的是您的 SearchInputTextStrategy 有两个类型参数,但实际上只使用一个...”两个?那么它的参数字符串和返回值T,NOT?
    • @Lisa:否,type 参数:TU。您使用T(作为返回类型)但从不使用U。为什么要在委托声明中包含U
    • @Jon 忘了 U 对不起它只是 => ublic 委托 IEnumerable SearchInputTextStrategy(string param);好的,乔恩可以说我创建了一个公共类 SearchProvider 我什至可以使用通用属性,那么是否可以将委托保存在字段中?现在的问题是 SearchInputTextStrategy 类型是 KNOWN no​​where...
    【解决方案2】:
    private SearchInputTextStrategy<T, string> _searchStrategy;
    
    public void BindElements<T, TDisplayProperty,TSortProperty>
    (
           IEnumerable<T> dataObjects,
           Func<T, TDisplayProperty> selectorDisplayMember,
           Func<T, TSortProperty> selectorSortMember,
           string delimiter,
           SearchInputTextStrategy<T, string> searchStrategy
    )
    {
        _searchStrategy = searchStrategy;
    }
    

    【讨论】:

    • 嘿大卫,你为什么使用 TDisplayProperty 而不是字符串?而且这不起作用=> SearchInputTextStrategy 作为字段,因为它不被称为类型
    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多