-1

How do i use reflection to call this method.

using System.Reflection

public static string NotSoObvius<V>(V show) where V : class
    {
        return string.Format("This is it", show);
    }
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25
Muro
  • 11
  • 1
  • We cannot see the class or struct in which the method is a member. But otherwise something like `var res = (string)typeof(Xxx).GetMethod("NotSoObvius").MakeGenericMethod(show.GetType()).Invoke(null, new[] { show, })` should be fine. – Jeppe Stig Nielsen Jun 14 '16 at 11:26
  • noted thank you, but i am not understanding a little – Muro Jun 14 '16 at 11:44

1 Answers1

-1

You should try something like this:

Type myType = Type.GetType("MyClass");
MethodInfo notSoObviusInfo = myType.GetMethod("NotSoObvius");
Type[] types = new Type[]{typeof(YourDesiredTypeHere)};
notSoObviusInfo = notSoObviusInfo.MakeGenericMethod(types);
string myReturn = (string)notSoObviusInfo.Invoke(null, new[]{new YourDesiredTypeHere()});

From: https://msdn.microsoft.com/pt-br/library/a89hcwhh(v=vs.110).aspx

  • But `V` was not an actual type, it was a type argument to the method. So `V` is not in scope above. And you want to use `MakeGenericType` to specify what the type argument should be (see my comment to the question). And a `static` method does not need a "this" target (your `magicClassObject`). So your answer has several issues. – Jeppe Stig Nielsen Jun 14 '16 at 13:54
  • You right. I just fixed my answer following MSDN tutorial: https://msdn.microsoft.com/pt-br/library/a89hcwhh(v=vs.110).aspx "If a constructor is static, this argument must be null or an instance of the class that defines the constructor." – Akram Mashni Jun 14 '16 at 17:40
  • 1
    Answer updated and it is now all functional! – Akram Mashni Jun 14 '16 at 23:21