4

I wrote a method like this in C#.

 MethodBase method = MethodBase.GetCurrentMethod();
 string key ="";
 for (int i = 0; i < method.GetParameters().Length; i++)
 {
     key=method.GetParameters().Name;
     // need value of parameter here             
 } 

I'm getting parameter names through the above code. My question is: How can I get the values of the parameters which are coming to my method?

1 Answer 1

5

You can't - not without using the debugger API, at least (which is distinctly non-trivial). That information isn't available via reflection. In particular, the MethodBase object you're fetching is probably going to be the same one on every invocation.

(I don't think the method you've written is quite as you've shown either, and I really hope you're not calling GetParameters inside a loop like that, but that's a side issue.)

Sign up to request clarification or add additional context in comments.

2 Comments

First of all,thanks for your interesting.As you mentioned,How can I write by using the debugger API?
@Selo: It's not something I've used myself, but have a look at pages like this - I don't know how much it's changed for .NET 4, either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.