1

I'd like to know whether I can retrieve an argument name from an argument value. I need this functionality because I've build myself a static class (called Requires) to make argument checking in method bodies a one-liner. Currently, the validation methods are implemented like this:

Requires.StringNotNullOrEmpty(string argName, string argValue) {...}

To validate an argument, you have to provide the name of the argument (later used to throw a meaningful ArgumentException) and its value.

My question is, is there a way to retrieve the argument name from an argument value inside a method body?

Thanks in advance and happy easter!

Mats
  • 14,902
  • 33
  • 78
  • 110
  • It's probably possible... However, your method won't necessarily know which one. So, you'd have to tell it which one you're talking about. And, now, you're only a step away from just telling it the name right off the bat, like in your example. – Mike Caron Apr 20 '11 at 08:03
  • Did you have a look at Code Contracts from Microsoft Research? It looks very much like what you describe. – Johann Blais Apr 20 '11 at 08:12

5 Answers5

1

I think you are looking for Reflection.

Reflection: How to Invoke Method with parameters

Community
  • 1
  • 1
Blazes
  • 4,721
  • 2
  • 22
  • 29
0
class A
{
    public void MyMethod(int num, string aString)
    { 
        ParameterInfo[] parameters = typeof(A).GetMethod("MyMethod", BindingFlags.Public|BindingFlags.Instance).GetParameters();
        string secondParameterName = parameters[1].Name;   //you will get aString
    }
}
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • This replaces a hardwired parameter name by a hardwired method name - at greate expense too. – Joe Apr 20 '11 at 09:06
0

No, you cannot know the name used by the calling code - because in many cases, what's been passed to your method doesn't have a name at all, e.g. it could be an expression, or a literal. So there's no general solution to this.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

No. Point. You know the name of the arbument (argName). You can not know what is was SET from because... that is not even part of the argument. It is part of the knowledge of the outer class, not the argument (which would return argName).

TomTom
  • 61,059
  • 10
  • 88
  • 148
0

Not sure if this is what you had in mind

internal class TestClass
{

private void DoSomething(string myArg)
{
    // returns the name of the argument = "myArg"
    string myArgName = GetArgumentName(() => myArg);
    // check
    System.Diagnostics.Debug.Assert(string.Compare("myArg", myArgName, System.StringComparison.InvariantCulture) == 0, "names do not match");
}


private static string GetArgumentName<T>(System.Linq.Expressions.Expression<System.Func<T>> argument)
{
    string argumentName = null;
    System.Linq.Expressions.MemberExpression body = (System.Linq.Expressions.MemberExpression)argument.Body;
    if (body.Member != null)
    {
        argumentName = body.Member.Name;
    }

    if (argumentName == null)
    {
        // could not retrieve argument name
    }

    return argumentName;
}

}

MaLio
  • 2,498
  • 16
  • 23