开发者

Pass a generic delegate as Method parameter in C#

I have this delegate declaration:

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

Lets assume I did create here the new SearchInputTextStrategy delegate and name it MyDelegate.

this is my method declaration:

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?

}

How can I do 1.) and 2.) ? :-)

UPDATE 2:

Ok thats what I have done so far:

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));
            }
        }
    }

UserControl:

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

Please read also my comments in the Code. What I want to achieve is pass the delegate to the searchProvider to save it somewhere... The code I write here I understand up to 50 % so please bear with me Generics are new to me although I use generic List for a long time ;P

UPDATE 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;
            }         
        }
    }

The scenario is, that the user has put the TextBoxUserControl in a MainWindow and he has to pass a delegate pointing to a searchMethod. I have implemented this with the SearchCustomers_Method. The problem is that C# can not resolve that:

    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>'    

Do you see the problem? In any case the User must pass a delegate with the same definition the BindElements Method has!


It's odd that your SearchInputTextStrategy has two type parameters but only actually uses one... but you just need to specify the type arguments in the parameter type. For example:

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

I've only guessed at what the type arguments should be - you haven't really said what you want the parameter to represent.

You won't be able to easily have a field of the right type in your class, because the class itself doesn't know the type parameters involved. It's possible that you should really be making your class generic, or make another class which is able to handle the delegates appropriately. Without any more information, it's very hard to know which.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜