Skip to main content
 首页 » 编程设计

c#之在 C# 中将泛型委托(delegate)作为方法参数传递

2024年02月23日16lidabo

我有这个委托(delegate)声明:

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

假设我确实在这里创建了新的 SearchInputTextStrategy 委托(delegate)并将其命名为 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 !!!      
        } 

另请阅读我在准则中的评论。我想要实现的是将委托(delegate)传递给 searchProvider 以将其保存在某个地方......我在这里编写的代码我理解高达 50%所以请耐心等待泛型对我来说是新的虽然我使用泛型列表很长时间;

更新 2:

公共(public)部分类 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 的委托(delegate)。我已经使用 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 方法具有相同定义的委托(delegate)!

请您参考如下方法:

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

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

我只是猜测参数的类型应该是什么——你还没有真正说出你希望参数委托(delegate)什么。

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