2

I have a project using C# .NET 2.0 (cannot use a higher version). I would like to get the name of the parameters of a method in a method called by the first one. If I call a method from the MyMethodsClass, I want to receive a string indicating which parameter is not valid, following some conditions indicated in the isValid method from the MyCheckClass class.

With the following code, I get a string that returns "Please check the 5 parameter" in case I would call the myMehtod(5,1,1). But I would like to obtain, "Please check the a parameter".

How could I do that? Thank you!

public MyMethodsClass {

    public string myMethod (int a, int b, int c) {

        return MyCheckClass.isValid(a,b,c);
    }

    public string myMethod2 (int d, int e) {
        return MyCheckClass.isValid(d,e);
    }

}


//Other file
public class MyCheckClass {

    public static string isValid (params object[] parameters) {

            StringBuilder result= new StringBuilder();          
            for (int i = 0; i < parameters.Length; i++)
            {
                object p = parameters[i];
                //Some checks...
                if (p == null || p.Equals("") || p != 5)
                {                    
                    result.Append("Please check the " + p + " parameter");
                }
            }
            return result.toString();               
    }
}
pablof
  • 313
  • 2
  • 4
  • 18
  • 1
    To get this clear, if myMethod2 is called like this myMethod2(someValue, someOtherValue); you want to have the name 'someValue' and 'someOtherValue'. Or do you want 'd' and 'e' wich are in the signature of the method? – RvdK Mar 04 '13 at 16:10
  • I want to have the names of the signature, yes. Like: "Please check the a parameter", "Please check the d parameter" – pablof Mar 04 '13 at 16:22

2 Answers2

3

You combine two things you already know how to do.

  1. How can I find the method that called the current method?
  2. How can you get the names of method parameters in C#?
Community
  • 1
  • 1
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • 1
    @Antonijn this is wat the OP wants. Please see my comment to the question and his reaction to it – RvdK Mar 04 '13 at 16:24
  • Yes, I think this is what I want. I am going to test it. Thank you : ) – pablof Mar 04 '13 at 16:33
  • I am afraid this is not enough. I cannot identify in the params array which parameters map to which arguments from the original function (for example, I could receive isValid(3,3,5), but inside the isValid method I do not know where this 5 comes from. I could get the original parameters name, but not the corresponding one. – pablof Mar 05 '13 at 09:29
  • 1
    The `i` variable tells you which parameter you are looking at. – Raymond Chen Mar 05 '13 at 13:32
  • Yes. Thank you Raymond. But at the first sight I did not consider that I could have some myMethod3 method with 7 arguments that calls the isValid method with only three of them, and this case makes it very messy as I would not have a way to know which of them were passed... I would need to pass a Dictionary mapping the variable name and its value, to isValid mehtod making it more difficult to use by other classes that would like to use the isValid method... – pablof Mar 05 '13 at 14:24
  • If you're going to be passing only a subset of the parameters, then you need to let `isValid` know which ones you passed. At that point, you may as well just pass the names directly since it's about the same amount of work. `MyCheckClass(new Parameter[] { { "a", a }, { "c", c }, { "e", e })`. – Raymond Chen Mar 05 '13 at 15:39
1

These two line should do the job for you, perhaps need to adapt but should work ^^

        MethodInfo info = typeof (MyMethodsClass).GetMethod("MethodName");
        string name = info.GetParameters()[indexOfTheFaultyOne].Name;
Emmanuel Istace
  • 1,209
  • 2
  • 14
  • 32