0
private ComplexAdvertismentsQuery QueryForm1(string searchQuery)
private ComplexAdvertismentsQuery QueryForm2(string searchQuery)
private ComplexAdvertismentsQuery QueryForm3(string searchQuery)
private ComplexAdvertismentsQuery 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.

6
  • What do you mean by dynamic? Please clarify. Commented Jan 19, 2011 at 8:20
  • I think you need to add a lot more context to this question; there are definitely people here who can give you DynamicMethod examples, but it is meaningless unless the question is very clear. Commented Jan 19, 2011 at 8:23
  • 1
    Calling methods through reflection can slow your program down. Wouldn't it be more useful to have an array of Func<string, ComplexAdvertismentsQuery> and iterate on it ? Commented Jan 19, 2011 at 8:38
  • @senzacionale, its not clear what you want to achieve by dynamic invocation here. Perhaps you should look at the way suggested by Nekresh. Commented Jan 19, 2011 at 8:43
  • i solve the problem by this example sadi02.wordpress.com/2008/05/14/… Commented Jan 19, 2011 at 9:26

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.