0

I need to call a static method (of a helper class) which has a generic type constraint like this:

public static ResultType SomeMethod<T>(string someParm) 
    where T : SomeBaseClass

How can I call this method if I only know T by its name (string)?

I have seen this, but I could not figure out how to use MethodBase.Invoke in my case.

UPDATE

After this was flagged as duplicate, I realised I should have asked if the static method is an async method with optional parameters.

public async static Task<ResultType> SomeMethod<T>(string someParm, 
    string optionalParm) where T : SomeBaseClass

Then I found this answer: .NET invoke an async method. Basically, invoking an async method would return what you expect it to return, which is generally either a Task or Task<>. Bleeding obvious now, LOL

Then, I was getting Parameter mismatch count. It turned out I had to provide all parameters, and specify Type.Missing for optional parameters where I want the method to use the default value.

Community
  • 1
  • 1
Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
  • 1
    How about [this answer](http://stackoverflow.com/a/232621/5240004)? – theB Sep 09 '15 at 01:32
  • Hi @theB, @Blorgbeard, what happens if the method I wish to call is async: `public async static TasK SomeMethodAsync(string someParm)`. I can't seem to work it out. – Frank Fajardo Sep 09 '15 at 04:31
  • Don't worry, I found this: http://stackoverflow.com/questions/16153047/net-invoke-async-method-and-await – Frank Fajardo Sep 09 '15 at 05:08

0 Answers0