당신이 찾고있는 것이 존재하지 않는다고 생각합니다. 당신이 할 수있는 가장 가까운 PARAMS입니다 :
MyMethod(params object[] args)
{
// if you have to do this, it's quite bad:
int intArg = (int)args[0];
string stringArg = (string)arg[1]:
}
// call with any number (and type) of argument
MyMethod(7, "tr");
아무 컴파일 타임 유형 검사가 없으며, 따라서 인수를 처리하는 모든 용도의 방법이 아닙니다. 그러나 이 인 경우 동적 인 경우입니다. 이는 아마도 해결책 일 것입니다.
편집 : 당신은 목록/사전에 수동으로 모든 인수를 둘 필요가
: 다른 생각을했다. 다음 있도록 헬퍼 클래스를 작성할 수 있습니다
MyMethod(int arg1, string arg2)
{
Arguments.Add(() => arg1);
Arguments.Add(() => arg2);
//
}
도우미는이
public static void Add<T>(Expression<Func<T>> expr)
{
// run the expression to get the argument value
object value = expr.Compile()();
// get the argument name from the expression
string argumentName = ((MemberExpression)expr.Body).Member.Name;
// add it to some list:
argumentsDic.Add(argumentName, value);
}
예. 너는 그들을 얻지 못할 것이다. 매개 변수 이름 만 가져옵니다. 그래서 나는 그것이 효과가 없을 것이라고 생각합니다. – Kirtan
@Robin : 사전에 쉽게 인수를 추가하기 위해 제 구현을 보았습니까? 나는 내 대답을 편집했다. 이게 도움이 되었습니까? –