0
아래에는 매개 변수가없는 매개 변수가있는 작업을 실행하는 데 도움이되는 몇 가지 방법이 있으며 작동합니다. 내가 없애 싶어 문제가 그러나 는 문제는 내가Dynmic 매개 변수 및 매개 변수없는 작업 실행
Execute<Action<Myclass>, Myclass>(ActionWithParameter);
를 호출 할 때 나는 MyClass
2 번 통과하고 있다는 점이다. 내 ActionWithParameter
작업에 필요한 매개 변수를 처음 정의하고 두 번째로 내 Execute<TAction, TType>
메서드에서 예상하는 매개 변수 유형을 정의합니다.
그럼 제 질문은 : 제 2 일반용 TType
을 없애고 어떻게 든 제네릭 TAction
에서 가져 오는 방법이 있습니까?
TAction<TType>
일까요?
class Program
{
static void Main(string[] args)
{
Execute<Action>(ActionWithoutParameter);
Execute<Action<Myclass>, Myclass>(ActionWithParameter);
Console.ReadLine();
}
private static void ActionWithoutParameter()
{
Console.WriteLine("executed no parameter");
}
private static void ActionWithParameter(Myclass number)
{
Console.WriteLine("executed no parameter " + number.ID);
}
private static void Execute<TAction>(TAction OnSuccess)
{
Execute<TAction, Myclass>(OnSuccess);
}
private static void Execute<TAction, TType>(TAction OnSuccess)
{
if (OnSuccess is Action)
{
Action method = OnSuccess as Action;
method();
}
else if (OnSuccess is Action<TType>)
{
Myclass myclass = new Myclass() { ID = 123 };
TType result = (TType)(object)myclass;
Action<TType> method = OnSuccess as Action<TType>;
method(result);
}
}
태그를 언어 태그로 바꾸기를 선택하는 것이 좋습니다. –
태그에 C#을 추가했습니다. 제안 해 주셔서 감사합니다. – Daniel