Solved

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

Posted on 2008-10-06
19
834 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
  • 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
 

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 125 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now