Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .NET reflection to determine method return type (including void) and parameters?

how to know the number and type of parameters?

how to know the return type?

how to check whether the return type is void?

like image 305
Louis Rhys Avatar asked Aug 11 '10 09:08

Louis Rhys


People also ask

How to check return type of the method in c#?

ReturnType property. To get the return type property, first get the Type . From the Type , get the MethodInfo . From the MethodInfo , get the ReturnType .

What is reflection where can it be used c#?

Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them.

What is reflection in asp net with example?

Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties.

What invoke () method do in C#?

This method dynamically invokes the method reflected by this instance on obj , and passes along the specified parameters. If the method is static, the obj parameter is ignored. For non-static methods, obj should be an instance of a class that inherits or declares the method and must be the same type as this class.


1 Answers

Use MethodInfo.ReturnType to determine the return type, and MethodBase.GetParameters() to find out about the parameters. (MethodInfo derives from MethodBase, so once you've got the MethodInfo via Type.GetMethod etc, you can use both ReturnType and GetParameters().)

If the method is void, the return type will be typeof(void):

if (method.ReturnType == typeof(void))
like image 128
Jon Skeet Avatar answered Oct 24 '22 06:10

Jon Skeet