У меня есть это объявление делегата:
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%, поэтому, пожалуйста, потерпите меня. Универсальные методы для меня новы, хотя я использую общий список в течение длительного времени; п
ОБНОВЛЕНИЕ 2:
открытый частичный класс MainWindow: Window { открытый делегат IEnumerable SearchInputTextStrategy (параметр строки);
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. Проблема в том, что С# не может решить это:
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!