Invoking generic method from Main using reflection

Purusartha

I am playing around with reflection. Not sure if anyone's come up with this scenario before:

    static void Main(string[] args)
    {

        MethodInfo definition = typeof(SafeClass).GetMethod("Print");
        MethodInfo constructed = definition.MakeGenericMethod(typeof(int));
        constructed.Invoke(null, null);

        Console.ReadLine();
    }

public class SafeClass
{
    public void Print<T>()
    {
        Console.WriteLine(typeof(T));
    }
}

I got the error Non-static method requires a target.

If I make the Print method static, the call goes through. However, I am unsure how to invoke this call from inside static method.

Rob

Print<T>() is an instance method, not a static method. It needs to be invoked on something.

For example, the code would be:

var sc = new SafeClass();
sc.Print<int>();

Just like you can't simply do this:

Print<int>();

You can't make reflection do it either. Either make print static, or change your code to this:

MethodInfo definition = typeof(SafeClass).GetMethod("Print");
MethodInfo constructed = definition.MakeGenericMethod(typeof(int));
constructed.Invoke(new SafeClass(), null);

Console.ReadLine();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Invoking generic method having generic type parameter using reflection

From Dev

Invoking a method from a RecyclerView obtained using reflection

From Java

Invoking a static method using reflection

From Dev

Invoking Integer.parseInt method using reflection

From Java

Invoking setter method using java reflection

From Java

Invoking Java main method with parameters from Eclipse

From Dev

Invoking main() method from parent class

From Dev

Invoking generic method/delegate from a non-generic base class

From Java

access exception when invoking method of an anonymous class using java reflection

From Java

Java invoking command line arguments from the main method to a separate method

From Dev

Calling generic method with constraints using reflection

From

How to call a generic async method using reflection

From Dev

Java Reflection, extract generic type from method

From Java

Java call main() method of a class using reflection

From Dev

How to execute generic method by setting double generic using reflection

From Dev

How to invoke a generic method with generic return type using reflection

From Dev

C# detect nullable reference type from generic argument of method's return type using reflection

From Dev

Reflection of generic method not working

From Dev

generic Caster for method reflection

From Dev

Invoking method of AutoGenerated class with Reflection.Emit

From Dev

How to pass an argument when invoking a method (reflection)?

From Java

Java reflection API: Invoking a method without parameters

From Java

Unknown number of arguments when invoking a reflection method

From Java

Why do I get "object is not an instance of declaring class" when invoking a method using reflection?

From Dev

PHP: Confusing disparity between invoking reflection method using constructed array or func_get_args() in version 5.4

From Dev

Using reflection to find all Classes of a Type(), ordering them, and invoking a named method

From Dev

Invoke method on generic base class using Reflection via derived type

From Dev

Ambiguous match found when using reflection to find Generic method

From Dev

Await the result of Task<TDerived> using reflection in a non-generic method

Related Related

  1. 1

    Invoking generic method having generic type parameter using reflection

  2. 2

    Invoking a method from a RecyclerView obtained using reflection

  3. 3

    Invoking a static method using reflection

  4. 4

    Invoking Integer.parseInt method using reflection

  5. 5

    Invoking setter method using java reflection

  6. 6

    Invoking Java main method with parameters from Eclipse

  7. 7

    Invoking main() method from parent class

  8. 8

    Invoking generic method/delegate from a non-generic base class

  9. 9

    access exception when invoking method of an anonymous class using java reflection

  10. 10

    Java invoking command line arguments from the main method to a separate method

  11. 11

    Calling generic method with constraints using reflection

  12. 12

    How to call a generic async method using reflection

  13. 13

    Java Reflection, extract generic type from method

  14. 14

    Java call main() method of a class using reflection

  15. 15

    How to execute generic method by setting double generic using reflection

  16. 16

    How to invoke a generic method with generic return type using reflection

  17. 17

    C# detect nullable reference type from generic argument of method's return type using reflection

  18. 18

    Reflection of generic method not working

  19. 19

    generic Caster for method reflection

  20. 20

    Invoking method of AutoGenerated class with Reflection.Emit

  21. 21

    How to pass an argument when invoking a method (reflection)?

  22. 22

    Java reflection API: Invoking a method without parameters

  23. 23

    Unknown number of arguments when invoking a reflection method

  24. 24

    Why do I get "object is not an instance of declaring class" when invoking a method using reflection?

  25. 25

    PHP: Confusing disparity between invoking reflection method using constructed array or func_get_args() in version 5.4

  26. 26

    Using reflection to find all Classes of a Type(), ordering them, and invoking a named method

  27. 27

    Invoke method on generic base class using Reflection via derived type

  28. 28

    Ambiguous match found when using reflection to find Generic method

  29. 29

    Await the result of Task<TDerived> using reflection in a non-generic method

HotTag

Archive