문제

I am creator dynamic reporting system that accept calls from javascript within browsers.

I have the following report class,

    [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
            }

        }
    }

and in the caller class i have

    public class caller{
          private void MyMethod(object obj){ //do something}

          report = new report(MyMethod);
    }

What i need to do, is after calling the constructor the report class should generate new method (which is COM visible) and give it the name as MyMethod, and inside it it should invoke the orginal MyMethod

    public static MyMethod(object obj)
    {
    // in here it should invoke the actions[0].invoke(obj) 
    }
도움이 되었습니까?

해결책

I would suggest you create a dictionary of each action and its name and then lookup the item in the dictionary by its name. then call the action.

    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);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top