【发布时间】: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