Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to programmatically get parameter names and values passed inside that method?

Posted on 2008-10-06
19
Medium Priority
?
975 Views
Last Modified: 2013-12-17
I use reflection to find the method name and parameters from within that method.

in C#, i can get using the code
MethodBase mb2 = System.Reflection.MethodBase.GetCurrentMethod();
ParameterInfo[] p = mb2.GetParameters();

However i cannot get the Parameter values. Is there a way to get the parameter values. I get the parameter names as strings when i use parameterinfo.
0
Comment
Question by:xribble
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
19 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22654141
gives an example on building a parameter list ....
http://zulfiqar.typepad.com/zulfiqars_web/2007/01/c_using_reflect.html
0
 

Author Comment

by:xribble
ID: 22655151
I am in a method already and have to figure out the parameters that were passed to the metod i was in. The code that u have sent need to created a new object with all parameters and then use the propertyinfo collection. thats not what i was looking for.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22659144
Please be more specific than...when you say parameter values, do you mean the value that the user is passing in? or the program is passing in?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:xribble
ID: 22660028
public void a()
{
    int x = b('silomone',24,'New York',42000);
}

public int b(string name, int age, string city, int salary)
{
      capturecurrent parameters();
}

public void capturecurrent parameters()
{
foreach parameter in the method,
print the parameter/value.

}
0
 
LVL 21

Accepted Solution

by:
silemone earned 500 total points
ID: 22660039
got you...
0
 
LVL 21

Expert Comment

by:silemone
ID: 22660198
well you would have to add those manually...such as in captureCurrentparameters(); pass the variable names and have an class or arraylist of hashmaps...class you can store all data in one shot...with the hashmap, you would only store key/value pair...but there nothing except using a Debug API or StackFrame class to take parameter off stack where it is located.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22660217
by the way, the Debug API is more complicated and StackFrame offer security problems since the it's not const, it can be changed or corrupted...unfortunately sometimes there's not a method to do things we want and we have to do them manually...
0
 

Author Comment

by:xribble
ID: 22660947
I am trying to write my errors to an error log using this approach. I have a huge code base and its very difficult to debug sometimes in production what is causing the behaviour, i was hoping to seed this kind of code snippet in the methods so that we could figure out what happened.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22661837
well you can, but you just have to do it manually...

1)  create ErrorLog Class... where  

use this type of structure:

public int b(string name, int age, string city, int salary)
try
{

}
catch (exception e)
{
      string errorMessage = e.StackTrace.ToString();
      ErrorLogger x = new ErrorLogger(name, age, city, salary, errorMessage)

}

private class ErrorLogger
{
        string name;
        int age;
        string city;
        int salary;
        string errorMessage;

         public ErrorLogger(string name, int age, string city, int salary, string errorMessage)
        {
                this.name = name;
                .
                .
                .
                this.errorMessage = errorMessage;
        }
        public ToString()
        {
               //print out what you would like to show....
        }
}

0
 
LVL 21

Expert Comment

by:silemone
ID: 22661872
by the way, ErrorLogger class is private because it will be inside your current class...otherwise make it public if you want to give it a separate file...

you could also use structs instead, or

hashmaps of each key/value....i.e.  hashmap nameError/error, hashmap ageError/error and store in an Arraylist or list and then iterate through it to get all error data....
0
 

Author Comment

by:xribble
ID: 22662078
I have thousands of methods in my code, i cant possibly keep adding this code. its too much of work. is it possible to define a string and get its value.

for ex.
int a = 1;
string myVar = "a";
int b = getValueOf(myVar)  maybe use reflection or something like that. That would fix my problem
0
 
LVL 21

Expert Comment

by:silemone
ID: 22662246
This is what i'm telling you to do...hey...i'm going to try to write a little method for this for you...

public void a()
{
    int x = b('silemone',24,'New York',42000);
}


public int b(string name, int age, string city, int salary)
{
     try
     {
              //whatever code that you want to occur
     }
     catch (Exception ex)
     {          
           capturecurrent parameters(name, age, city, salary, ex.StackTrace.ToString());
           return;
     }
}

public void capturecurrent parameters(string name, int age, string city, string salary, string errorMessage)
{
            string name;
            int age;
            string city;
            string salary;
            string errorMessage;

            this.name = name;
            this.age = age;
            this.city = city;
            this.salary = salary;
            this.errorMessage = errorMessage;

}

if you want to use your code below:

int a = 1;
string myVar = a.ToString();
int b = Convert.ToInt32(getValueOf(myVar));
0
 

Author Comment

by:xribble
ID: 22662525
Dude,
I want to avoid writing a new method everytime/

public void capturecurrent parameters(string name, int age, string city, string salary, string errorMessage)
{
            string name;
            int age;
            string city;
            string salary;
            string errorMessage;

            this.name = name;
            this.age = age;
            this.city = city;
            this.salary = salary;
            this.errorMessage = errorMessage;

}

this is ridiculous. I dont mean to hurt u, i mean the coding wise. Who has time to write a method for each method in the codebase. it has to be generic in some form or sort of.
0
 

Author Comment

by:xribble
ID: 22662535
Do u get my point. I think u are not getting the need of the problem.
0
 
LVL 21

Expert Comment

by:silemone
ID: 22663015
Your rudeness is not appreciated.  you're not explaining yourself very clearly and it seems you're very confused...i understand the purpose of reflection...but please READ your statement above and try to decipher.  Do not get frustrated if your statements are not clearly written...your last statement is the most clear statement you've made thus far and finally explains your purpose.

I think when someone is trying to help you, in the future you should be mindful of your manners.  We're all professionals here and should accordingly.  If I'm not understanding you clearly, and read your statement above, then just try to communicate the problem better.

0
 

Author Comment

by:xribble
ID: 22663095
i dont need to learn manners from you, as i mentioned its not to hurt you personally. it is ridiculous to write a method for each and every method i write, I had code written by 12 developers for the past 6,7 years. Can u understand the size of the code base. the comment is not meant to disrespect, and i am sorry if you misunderstood that statement.
0
 

Author Comment

by:xribble
ID: 22663100
Dude i still need the solution.
0
 

Author Comment

by:xribble
ID: 22693038
Yes
0
 
LVL 3

Expert Comment

by:Mahmoud_Al_Hattab
ID: 32630486
dears... I know I am late to interrupt.. but I needed what xribble is asking about.

Actually to be honest, I could understand what xribble wants exactly. Maybe because I am looking for the same thing.

but alas I could not find any solution so far. According to my investigations, it is not possible. I have been diving into inside the stack and the IL, but could not find clues!

if you really find anything it would be great :) you can contact me on: mah.hat@gmail.com or through EE.

calm down guys :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question