question

ShwetaPaatil avatar image
0 Votes"
ShwetaPaatil asked DanielZhang-MSFT commented

Reflection: How to Invoke Method with parameters and value of parameters at runtime.

HI,

I am trying to invoke a method via reflection with parameters with parameter value.
I have used below code:

                 var methodInfo = propertyInfo.GetMethod(propertyOrMethodName);
                 propertyValue = methodInfo?.Invoke(CreateInstance, methodInfo.GetParameters());

it is giving error :
System.ArgumentException: 'Object of type 'System.Reflection.RuntimeParameterInfo' cannot be converted to type 'System.Boolean'.'

I want to access parameter value at run time.
Any help will be appreciated.

Regards
Shweta

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi ShwetaPaatil,
First of all, I suggest you refer to the code example in this [thread][1] to find your code problem.
Note that if there are multiple types in the parameter, the array should be object [].
If not successful, please provide more code to reproduce the problem.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi Daniel,

Thanks for your response.

I went through the given thread. but it is not resolving my problem.
Actually when i do methodInfo.GetParameters() it gives me type and name of parameters but i want value of that parameter as well so i can pass it as object[] while invoking method. i don't want to pass there hardcoded values.

Is there any way to get parameter value at runtime.


Best regards
Shweta

0 Votes 0 ·

Hi @ShwetaPaatil,
To get the value, you need to use PropertyInfo.GetValue method.

 var value = PropertyInfo.GetValue(instance);

Best Regards,
Daniel Zhang



0 Votes 0 ·

HI Daniel,

Thanks for your response.

var value = PropertyInfo.GetValue(instance);
This will give value of property not for method parameters.

var value= methodInfo.GetParameters());

This is giving parameter information of method which consists parameter name and type but not the value of that parameter. I want value of that parameter so I can pass it as new object[] while invoking method.

Regards
Shweta



0 Votes 0 ·

Show an example of value you want to get.

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi ShwetaPaatil,
First, sorry for my misunderstanding.
And you cannot use reflection to get the parameter value of a method. Because reflection will return metadata information. If you want to get the value of a specific field or attribute, you also need to use an instance.
There are many ways to get the value of the parameter using the internal pipeline of the .NET framework, namely the profiler API and the debugger API.
You can do it with AOP.
For more detailed explanation, you can also refer to the following link.
How can I get the values of the parameters of a calling method?
How to get the parameter value using reflection
You can also try the PostSharp to achieve it.
Can I get parameter names/values procedurally from the currently executing function?
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.