我是创建者动态报告系统,接受浏览器中来自javascript的调用。
我有以下报告类
[ComVisible(true)]
public class Report
{
public Report(IEnumerable<Action<object>> actions)
{
foreach (var action in actions)
{
//here i want to create new methods that are public and have method name as the action method name
}
}
}
并且在调用者类中我有
public class caller{
private void MyMethod(object obj){ //do something}
report = new report(MyMethod);
}
我需要做的是,在调用构造函数之后,报表类应该生成新方法(COM可见)并将其命名为MyMethod,并在其中调用原始MyMethod
public static MyMethod(object obj)
{
// in here it should invoke the actions[0].invoke(obj)
}
答案 0 :(得分:1)
我建议您创建每个操作及其名称的字典,然后按字母名称查找字典中的项目。然后调用该动作。
Dictionary<string, Action<object>> _methodDictionary = new Dictionary<string, Action<object>>();
public void Report(IEnumerable<Action<object>> actions)
{
foreach (var action in actions)
{
// you need to get your name somehow.
_methodDictionary.Add(action.GetType().FullName, action);
}
}
public void callMethod(string actionName, object itemToPass)
{
if(_methodDictionary.ContainsKey(actionName))
_methodDictionary[actionName].Invoke(itemToPass);
}