ArgumentException 객체를 만드는 데 필요한 매개 변수에 함수 인수를 어떻게 바인드합니까?
-
15-11-2019 - |
문제
나는 종종 이와 같이 인수를 가져야하는 기능을 가지고 있습니다 :
Public Shared Function F(ByVal address as String)
기본적으로 new argumentException ( "잘못된 주소!", "주소")
이제는 로컬 변수의 이름을 바꾸기 위해 내장 된 리팩토링 도구를 사용하는 경우 (ADDR로 주소의 이름을 바꾸겠습니다), 내 인수 예외의 문자열은 여전히 "ADDR"가 아닙니다.
이 문제에 가장 좋은 해결책이 무엇인지 궁금합니다.(수동으로 추적 또는 CTRL-F 검색으로 다른 것)
해결책
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));
}
}
다른 팁
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.
제휴하지 않습니다 StackOverflow