Ich weiß, das ist ein alter Beitrag, aber in diesen Situationen finde ich Attribute und eine Fabrik sehr praktisch.
Der folgende Code verwendet ein benutzerdefiniertes Attribut (Command
), mit dem Sie Ihre Methoden zuordnen können, indem Sie einen String-Wert angeben, wie sie auf Sie reagieren sollen.
Die Factory-Methode verwendet Reflektion, um ein Wörterbuch dieser Methoden zu generieren, und ruft es auf, wenn Sie CommandFactory
aufrufen.
Die Dinge könnten etwas aufgeräumt werden, Aufruf von Aufruf ist ein wenig hässlich, aber es hängt nur davon ab, wie Sie den Code ausführen möchten.
using System.Collections.Generic;
using System.Linq;
namespace MyApp
{
using System.Reflection;
using MyApp.Commands;
class Program
{
static void Main(string[] args)
{
var methods = new MyCommands();
MethodInfo myMethod;
myMethod = CommandFactory.GetCommandMethod("Show Commands");
myMethod.Invoke(methods, null);
myMethod = CommandFactory.GetCommandMethod("Close window");
myMethod.Invoke(methods, null);
myMethod = CommandFactory.GetCommandMethod("Switch window");
myMethod.Invoke(methods, null);
}
}
public static class CommandFactory
{
private static Dictionary<string, MethodInfo> speechMethods = new Dictionary<string, MethodInfo>();
public static MethodInfo GetCommandMethod(string commandText)
{
MethodInfo methodInfo;
var commands = new MyCommands();
if (speechMethods.Count == 0)
{
var methodNames =
typeof(MyCommands).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<CommandAttribute>().Any());
foreach (var speechAttributeMethod in speechAttributeMethods)
{
foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
{
speechMethods.Add(((CommandAttribute)attribute).Command, speechAttributeMethod);
}
}
methodInfo = speechMethods[commandText];
}
else
{
methodInfo = speechMethods[commandText];
}
return methodInfo;
}
}
}
namespace MyApp.Commands
{
class MyCommands
{
[Command("Show All")]
[Command("Show All Commands")]
[Command("Show commands")]
public void ShowAll()
{
ProgramCommands.ShowAllCommands();
}
[Command("Close Window")]
public void CloseWindow()
{
ControlCommands.CloseWindow();
}
[Command("Switch Window")]
public void SwitchWindow()
{
ControlCommands.SwitchWindow();
}
}
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
public class CommandAttribute : System.Attribute
{
public string Command
{
get;
set;
}
public CommandAttribute(string textValue)
{
this.Command = textValue;
}
}
}
sind alle Funktionen mit der gleichen Signatur? – elyashiv
Dies könnte besser für http://codereview.stackexchange.com/ geeignet sein. –
Wann immer Sie Code schreiben, der eine * string * verwendet, um eine Methode darzustellen, sollten Sie zuerst "Delegate" denken. –