how to know the number and type of parameters?
how to know the return type?
how to check whether the return type is void?
ReturnType property. To get the return type property, first get the Type . From the Type , get the MethodInfo . From the MethodInfo , get the ReturnType .
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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With