private ComplexAdvertismentsQuery QueryForm1(string searchQuery)
private ComplexAdvertismentsQuery QueryForm2(string searchQuery)
private ComplexAdvertismentsQuery QueryForm3(string searchQuery)
private ComplexAdvertisments开发者_如何学CQuery QueryForm4(string searchQuery)
...
then i check
query = QueryForm1(searchQuery);
if (query != null)
{
}
query = QueryForm2(searchQuery);
if (query != null)
{
}
can i make this dynamic?
i look here http://msdn.microsoft.com/en-us/library/exczf7b9.aspx and try with Type but this is not class it is jut method.
You can do that with an array of strong-typed delegates and iterate on it to execute all your methods.
var listOfQueries = new List<Func<string, ComplexAdvertismentsQuery>> {
QueryForm1, QueryForm2, QueryForm3, QueryForm4
};
foreach (var queryForm in listOfQueries) {
var query = queryForm(searchQuery);
if (query != null) {
// do something
}
}
If needed, you can populate the list by used reflection and get the corresponding delegate for each one, and pay the cost for reflection only once.
The drawback of this method is that all your methods must have the same prototype (ComplexAdvertismentsQuery method(string)
here).
精彩评论