在C#中将通用委托作为Method参数传递

时间:2010-11-28 20:34:31

标签: c# parameters methods delegates

我有这个委托声明:

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 !!!     
        }

请阅读我在“准则”中的评论。我想要实现的是将委托传递给searchProvider以将它保存在某个地方...我在这里写的代码我理解高达50%所以请耐心等待尽管我使用泛型List很长一段时间,但是泛型对我来说是新的。 p

更新2:

public partial class MainWindow:Window     {         public delegate 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方法具有!

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分: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;
}