简体   繁体   English

使用正确的参数动态调用方法

[英]Calling a method dynamically with the right arguments

first of all, the answer is not 首先,答案不是

.Invoke("name", new object[] { });  

:) that being said, i have some old generic code that creates context menus and hot keys handlers using definitions from a config file and executes the actual methods using .Invoke() where the method signature is pre-defined or else the method is not executed :)话虽如此,我有一些旧的通用代码,这些代码使用配置文件中的定义创建上下文菜单和热键处理程序,并使用.Invoke()执行实际方法,其中方法签名是预定义的,否则方法不是被执行

eg the method signatures are all same with these arguments 例如,方法签名与这些参数都相同

   change_back_color( mycontext ctx, object sender, controlItemClickEventArgs e)
   {
           //...           
   }

I have to rework the code to include some additional functionality and would like to have Unity/MEF like functionality where the signature can include the required parameters only and order of arguments can be changed 我必须重新编写代码以包括一些其他功能,并希望拥有类似Unity / MEF的功能,其中签名只能包含所需的参数,并且参数的顺序可以更改

eg the code can be changed to 例如,代码可以更改为

   [FunctionKey("myKey1")]
   change_back_color( object sender, mycontext ctx )
   {
           //...           
   }

or 要么

   [FunctionKey("myKey1")]
   change_back_color( mycontext ctx)
   {
           //...           
   }

Looking for guidance on how to go about it/where to look 寻找有关如何/在哪里寻找的指导

UPDATE - the definitions are in db which can be retrieved like so 更新-定义在db中,可以像这样检索

var commands = dbContext.GetCommands("current_view_name") ; // return method key, user roles etc.

// and i can use the following to match to "current_view_name"
[ImportMany(RequiredCreationPolicy=CreationPolicy.NonShared)]
public IEnumerable<Lazy<MenuItem,IDictionary<string,object>> MenuItems
{
   set
   {
        var fooMenuItems = value
            .Where(x => x.Metadata["ContextTarget"] == "current_view_name")
            .Select(x => x.Value);
        // attach fooMenuItems to some context menu...
    }
}

but these methods need some custom arguments as well ! 但是这些方法也需要一些自定义参数! any ideas ? 有任何想法吗 ?

If you want to dynamically add context menus to your application via MEF, I would do something like this: 如果您想通过MEF将上下文菜单动态添加到您的应用程序中,我将执行以下操作:

[Export(typeof(MenuItem)]
[ExportMetadata("ContextTarget", "foo")]
public FooMenuItemForBarAction
{
    get
    {
        var menuItem = new MenuItem("BarAction");
        menuItem.Click += delegate
        {
            // code to handle click here
        }
        return menuItem;
    }
}

And then elsewhere you can import all the context menu items for "foo" like this: 然后可以在其他位置导入“ foo”的所有上下文菜单项,如下所示:

[ImportMany(RequiredCreationPolicy=CreationPolicy.NonShared)]
public IEnumerable<Lazy<MenuItem,IDictionary<string,object>> MenuItems
{
   set
   {
        var fooMenuItems = value
            .Where(x => x.Metadata["ContextTarget"] == "foo")
            .Select(x => x.Value);
        // attach fooMenuItems to some context menu...
    }
}

For hotkey handlers you can do something similar: change the exported/imported type into an Action and replace the metadata by the key combo. 对于热键处理程序,您可以执行类似的操作:将导出/导入的类型更改为Action然后用键组合替换元数据。 Then when the user hits a key combo, you can scan all the imports for the one with the correct metadata and execute that action. 然后,当用户按下组合键时,您可以使用正确的元数据扫描所有导入,并执行该操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM