의 대리자를 만들고 나는이나는 몇 가지 단위 테스트를 쓰고 있어요 일반적인 기능
SomeTestHelperMethod<int, int>(0, 1);
SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5));
SomeTestHelperMethod<int, string>(2, "The quick brown fox jumped over the lazy dog.");
SomeTestHelperMethod<object, int>(new NullReferenceException(), 15);
SomeTestHelperMethod<object, object>(StringComparison.Ordinal, new Version());
SomeTestHelperMethod<object, string>((ushort)3, string.Empty);
SomeTestHelperMethod<string, int>(string.Empty, 195);
SomeTestHelperMethod<string, object>("A string", this);
SomeTestHelperMethod<string, string>("Another string", "Another string");
같은 다양한 인수와 함께 반복적으로 전화 드렸습니다 형태
public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value)
의 기능을 많이 가지고
내가하고 싶은 것은 액션 델리게이트를 취하고 모든 다른 인수로 델리게이트를 호출 할 수있는 함수를 작성하는 것이다. 그것을 할 방법이 있습니까? 나는 아직도 더 일반적인 방법에 관심이있을 것
private void CallWithKeyAndValue(string methodName)
{
MethodInfo method = typeof(ObservableDictionaryTest).GetMethod(methodName);
foreach (KeyValuePair<object, object> kvp in ourKeyValueSet)
{
MethodInfo genericMethod = method.MakeGenericMethod(kvp.Key.GetType(), kvp.Value.GetType());
genericMethod.Invoke(this, new[] { kvp.Key, kvp.Value });
}
}
그러나 이것은 내 목적을 위해 작동 : MichaelCG에
감사합니다 여기에 내가하고 결국 무엇을 : 대답
.
당신은 너무 당신이 그것의 개체 및 단지 루프의 목록을 가지고 있지만, 실제로 방법의 특정 제네릭을 호출 할 수 있습니다 의미? – MichaelGG
그런 식으로. 결국 나는 임의의 것들보다 더 나은 테스트 논증을 만들고 싶습니다. 그리고 그것을 한 곳에서이 폼의 모든 기능에 추가 할 수 있기를 바랍니다. 지금 당장 유닛 테스트에서 중복 제거 된 코드를 많이 가지고 있지만 제거하고 싶습니다 (SomeTestHelperMethod)는 모든 일반 함수입니다. –