Wie bindet ich Funktionsargumente an die Parameter, die ich bei der Erstellung eines ArgumentsException-Objekts liefert?

StackOverflow https://stackoverflow.com/questions/6042374

Frage

Ich habe oft Funktionen, die Argumente wie folgt:

Public Shared Function F(ByVal address as String)

Ich möchte also im Grunde neue ArgumentException ("Ungültige Adresse!", "Adresse") 0

Ich frage mich, was für dieses Problem die beste Lösung ist?(außer manuell Tracking oder mit Strg-F-Suchen)

War es hilfreich?

Lösung

In C#, you can do something like that:

static string GetMemberName<T>(Expression<Func<T>> expr)
{
  var body = ((MemberExpression)expr.Body);
  return body.Member.Name;
}

You would then use it like this:

static void Test(string someParam)
{
    if (someParam == null) { 
        throw new ArgumentNullException(GetMemberName(() => someParam)); 
    }
}

Andere Tipps

ReSharper can handle this. It can also rename occurrences of variables in strings.
However, I am not sure, how good their VB.NET support is.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top