2017-05-02 1 views
-1

명령 (문자열)이 키이고 명령이 값으로 매핑되는 함수로 사전을 만들어야합니다. 내가 직면 한 문제는 명령이 매핑되는 기능이 서로 다른 유형의 매개 변수를 가지고 있다는 것입니다.값으로 알 수없는 매개 변수가있는 사전이있는 사전

어떻게해야합니까? 가급적이면 람다 식으로 함수를 추가 할 수 있습니다.

Ex. dictionary.Add("square", x => x * x).

EDIT : 매핑 된 모든 함수는 void를 반환합니다.

+3

:

public class Command1 : ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { throw new NotImplementedException(); } public void Execute(object parameter) { //user your first lambda here } } public class Command2 : ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { throw new NotImplementedException(); } public void Execute(object parameter) { //use your second lambda here } } 

그런 다음 그들에게이 방법을 사용 다음은 샘플입니다 , double>'등)? 키로 값을 얻은 후에 값을 캐스팅 할 유형을 어떻게 알 수 있습니까? – Sweeper

+0

명령이 하드 코드되어 있으므로 추가해야하는 명령을 알고 있습니다. 이 기능을 사전에 매핑해야하는 시험에 대한 과제입니다. –

+0

@ K.Vestermark 호출자가 자신의 지식과 지식을 바탕으로 하드 코드를 작성하게하려는 경우 일반적인 데이터 구조를 사용하지 않아도됩니다. – Servy

답변

0

Dictionary<string, ICommand>을 사용하지 않는 이유 값이 다른 유형 (있는 경우`Func을 <더블, 더블>`,`Func을 <더블 더블이 유용 어떻게

Dictionary<string, ICommand> dictionary = new Dictionary<string, ICommand>(); 

dictionary.Add("Command1", new Command1()); 

dictionary.Add("Command2", new Command2()); 
관련 문제