Link to home
Start Free TrialLog in
Avatar of xribble
xribble

asked on

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

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.
Avatar of silemone
silemone
Flag of United States of America image

gives an example on building a parameter list ....
http://zulfiqar.typepad.com/zulfiqars_web/2007/01/c_using_reflect.html
Avatar of xribble
xribble

ASKER

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.
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?
Avatar of xribble

ASKER

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.

}
ASKER CERTIFIED SOLUTION
Avatar of silemone
silemone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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...
Avatar of xribble

ASKER

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.
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....
        }
}

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....
Avatar of xribble

ASKER

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
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));
Avatar of xribble

ASKER

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.
Avatar of xribble

ASKER

Do u get my point. I think u are not getting the need of the problem.
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.

Avatar of xribble

ASKER

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.
Avatar of xribble

ASKER

Dude i still need the solution.
Avatar of xribble

ASKER

Yes
Avatar of Mahmoud Al-Hattab
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 :)