首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >字典中的C#存储函数

字典中的C#存储函数
EN

Stack Overflow用户
提问于 2010-11-20 23:42:39
回答 4查看 105.2K关注 0票数 97

如何创建一个可以存储函数的字典?

谢谢。

我有关于30+的功能,可以从用户执行。我希望能够以这种方式执行函数:

代码语言:javascript
复制
   private void functionName(arg1, arg2, arg3)
   {
       // code
   }

   dictionaryName.add("doSomething", functionName);

    private void interceptCommand(string command)
    {
        foreach ( var cmd in dictionaryName )
        {
            if ( cmd.Key.Equals(command) )
            {
                cmd.Value.Invoke();
            }
        }
    }

但是,函数签名并不总是相同的,因此具有不同数量的参数。

EN

回答 4

Stack Overflow用户

发布于 2010-11-21 01:00:07

嘿,我希望这能帮上忙。你来自哪种语言?

代码语言:javascript
复制
internal class ForExample
{
    void DoItLikeThis()
    {
        var provider = new StringMethodProvider();
        provider.Register("doSomethingAndGetGuid", args => DoSomeActionWithStringToGetGuid((string)args[0]));
        provider.Register("thenUseItForSomething", args => DoSomeActionWithAGuid((Guid)args[0],(bool)args[1]));


        Guid guid = provider.Intercept<Guid>("doSomethingAndGetGuid", "I don't matter except if I am null");
        bool isEmpty = guid == default(Guid);
        provider.Intercept("thenUseItForSomething", guid, isEmpty);
    }

    private void DoSomeActionWithAGuid(Guid id, bool isEmpty)
    {
        // code
    }

    private Guid DoSomeActionWithStringToGetGuid(string arg1)
    {
        if(arg1 == null)
        {
            return default(Guid);
        }
        return Guid.NewGuid();
    }

}
public class StringMethodProvider
{
    private readonly Dictionary<string, Func<object[], object>> _dictionary = new Dictionary<string, Func<object[], object>>();
    public void Register<T>(string command, Func<object[],T> function)
    {
        _dictionary.Add(command, args => function(args));
    }
    public void Register(string command, Action<object[]> function)
    {
        _dictionary.Add(command, args =>
                                     {
                                         function.Invoke(args);
                                         return null;
                                     } );
    }
    public T Intercept<T>(string command, params object[] args)
    {
        return (T)_dictionary[command].Invoke(args);
    }
    public void Intercept(string command, params object[] args)
    {
        _dictionary[command].Invoke(args);
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-05-27 12:11:09

定义字典并添加函数引用作为值,使用System.Action作为类型:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;

public class Actions {

    public Dictionary<string, System.Action> myActions = new Dictionary<string, System.Action>();

    public Actions() {
        myActions ["myKey"] = TheFunction;
    }

    public void TheFunction() {
        // your logic here
    }
}

然后使用以下命令调用它:

代码语言:javascript
复制
Actions.myActions["myKey"]();
票数 2
EN

Stack Overflow用户

发布于 2013-07-01 18:21:40

为什么不对方法参数使用params object[] list,并在您的方法(或调用逻辑)中进行一些验证,它将允许可变数量的参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4233536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档