Hallo,
Über MethodInfo und CreateDelegate kann ich eine Action erstellen und kann diese auf das das CommandProperty eines MenuItems setzen. Das ganze funktionert gut, so lange die aufzurufende Methode keine Parameter hat. Was muss ich machen, damit das ganz auch funktioniert wenn Parameter im Spiel sind.
Hier der SourceCode:
private DelegateCommand CreateCommand(object tagetObj, string methodName, string canMethodName)
{
Type thisType = tagetObj.GetType();
MethodInfo theMethod = thisType.GetMethod(methodName);
MethodInfo canMethod = thisType.GetMethod(canMethodName);
Action action = (Action)Delegate.CreateDelegate(typeof(Action), tagetObj, theMethod);
Func<bool> func = (Func<bool>)Delegate.CreateDelegate(typeof(Func<bool>), tagetObj, canMethod);
return new DelegateCommand(action, func);
}
public bool CanShowContact()
{
return true;
}
// hiermit geht es
public void ShowContact()
{
System.Windows.MessageBox.Show("ShowContact erfolgreich aufgerufen!");
}
public bool CanShowContactWithParameter()
{
return true;
}
// hiermit geht es nicht
public void ShowContactWithParameter(string _inName)
{
System.Windows.MessageBox.Show("ShowContact erfolgreich aufgerufen mit "+_inName+" !!" );
}
Danke für die Unterstützung
Die Frage für mich ist erst einmal wozu du sowas brauchst?
Den wirklich Sinn macht sowas aus meiner Sicht irgendwie nicht.
Ein Delegate musst du dir immer selbst bauen.
Aber wenn der Einsatzzweck klar ist, könnte man vielleicht eine bessere Lösung finden als die aktuelle.
Den für mich sieht das aus, als würdest du dir nur zukünftig Arbeit beim Programmieren sparen wollen und dir dynamisch irgendwelche Delegates erstellen.
Der Sinn dahinter ist aber nicht klar.
Aktuell würde ich sagen, ist es den Aufwand nicht Wert.
T-Virus
Developer, Developer, Developer, Developer....
99 little bugs in the code, 99 little bugs. Take one down, patch it around, 117 little bugs in the code.
Danke Dir erstmal für Deine Antwort. Folgendes habe ich vor. Ich speichere in einer Datenbank (zur Menusteuerung) den Namen einer Methode. Aus dieser Methode möchte dann eine Action generieren um diese dem Command Property eines MenuItems zuzuseisen. Das ganz geschieht dann via MethodInfo und DelegateCommand.
Nun kann es sein, dass eine Mehtode Parameter hat, und genau da is mein Problem, ich weiss nicht wie ich das bei der Action machen muss.
Action action = (Action)Delegate.CreateDelegate(typeof(Action), tagetObj, theMethod);
Func<bool> func = (Func<bool>)Delegate.CreateDelegate(typeof(Func<bool>), tagetObj, canMethod);
Bei Func<bool> ... funktioniert es .
Danke, vielleicht fällt ja jemanden noch etwas ein .
Hallo,
das Problem ist, dass deine Methode "ShowContactWithParameter" nicht die Signatur eines Action-Delegates hat, sondern die Signatur von Action<string>.
Aber bei diesem Ansatz muss dann natürlich auch jemand eine Parameter übergeben. Ich sehe nicht, wie das über die Zuweisung an ein MenuItem sinnvoll möglich sein soll.
Hallo zusammen,
Das Problem hat sich erledig, hier die Lösung:
public bool CanShowContact()
{
return true;
}
public void ShowContact()
{
System.Windows.MessageBox.Show("ShowContact erfolgreich aufgerufen!");
}
public bool CanShowContactWithParameter(object param)
{
return true;
}
public void ShowContactWithParameter(string _inName)
{
System.Windows.MessageBox.Show("ShowContact erfolgreich aufgerufen mit "+_inName+" !!" );
}
public MainWindow()
{
InitializeComponent();
btnSave.Command = CreateCommand(_contact, "ShowContact", "CanShowContact");
btnCancel.Command = CreateCommand<string>(_contact, "ShowContactWithParameter", "CanShowContactWithParameter");
btnCancel.CommandParameter = "Hans ist der Parameter";
}
{
Type thisType = tagetObj.GetType();
MethodInfo theMethod = thisType.GetMethod(methodName);
MethodInfo canMethod = thisType.GetMethod(canMethodName);
Action action = (Action)Delegate.CreateDelegate(typeof(Action), tagetObj, theMethod);
Func<bool> func = (Func<bool>)Delegate.CreateDelegate(typeof(Func<bool>), tagetObj, canMethod);
return new DelegateCommand(action, func);
}
private DelegateCommand<t> CreateCommand<t>(object tagetObj, string methodName, string canMethodName)
{
Type thisType = tagetObj.GetType();
MethodInfo theMethod = thisType.GetMethod(methodName);
MethodInfo canMethod = thisType.GetMethod(canMethodName);
Action<t> action = (Action<t>)Delegate.CreateDelegate(typeof(Action<t>),tagetObj, theMethod);
Func<t,bool> func = (Func<t,bool>)Delegate.CreateDelegate(typeof(Func<t,bool>), tagetObj, canMethod);
return new DelegateCommand<t>(action,func);
}