文字列コードでメソッドを実行できるクラスを作成しました (これは、(既存の) データベースにこれらのコードが含まれているため、実際に必要なものです。ただし、データベース レコードを追加するにつれて、これをより拡張できるようにしたいと考えています。今のところ、コードごとにメソッドを作成する必要がありますが、このクラスを編集せずに作成できるインターフェイスとクラスを使用したいのですが、何か提案はありますか?
public class SpecificAction
{
public ActionToPerform ActionToPerform { get; private set; }
public string Action { get { return ActionToPerform.Action.Code; } }
public SpecificAction(ActionToPerform actionToPerform)
{
ActionToPerform = actionToPerform;
}
public dynamic Execute(object[] parametersArray = null)
{
Type type = typeof (SpecificAction);
MethodInfo methodInfo = type.GetMethod(Action);
dynamic result = null;
if (methodInfo != null)
{
ParameterInfo[] parameters = methodInfo.GetParameters();
result = methodInfo.Invoke(this, parameters.Length == 0 ? null : parametersArray);
}
return result != null && result;
}
//"Restart" is one of the codes in the database
public bool Restart()
{
throw new NotImplementedException();
}
//"AddToEventLog" is one of the codes in the database
public bool AddToEventLog()
{
if (!EventLog.SourceExists("Actions"))
{
EventLog.CreateEventSource("Actions", "Application");
}
EventLog.WriteEntry("Actions", Action + "is executed", EventLogEntryType.Warning);
return true;
}
//"SendEmail" is one of the codes in the database
public bool SendEmail()
{
throw new NotImplementedException();
}
}
私はこれを次のように呼び出し、動作します:
bool isPerformed = false;
var action = new SpecificAction(actionToPerform);
isPerformed = action.Execute();
ただし、可能なアクションごとにクラスを実装し、そこでメソッドを動的に実行する方が良いと思います。既存のパターンのいくつかでこれは可能ですか?私はすでに多くのことを試したので、例を挙げていただけますか?