• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1142
  • Last Modified:

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.
0
xribble
Asked:
xribble
  • 9
  • 9
1 Solution
 
silemoneCommented:
gives an example on building a parameter list ....
http://zulfiqar.typepad.com/zulfiqars_web/2007/01/c_using_reflect.html
0
 
xribbleAuthor Commented:
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
 
silemoneCommented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
xribbleAuthor Commented:
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
 
silemoneCommented:
got you...
0
 
silemoneCommented:
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
 
silemoneCommented:
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
 
xribbleAuthor Commented:
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
 
silemoneCommented:
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
 
silemoneCommented:
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
 
xribbleAuthor Commented:
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
 
silemoneCommented:
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
 
xribbleAuthor Commented:
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
 
xribbleAuthor Commented:
Do u get my point. I think u are not getting the need of the problem.
0
 
silemoneCommented:
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
 
xribbleAuthor Commented:
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
 
xribbleAuthor Commented:
Dude i still need the solution.
0
 
xribbleAuthor Commented:
Yes
0
 
Mahmoud_Al_HattabCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now