2014-01-15 16:17 阅读 19


I'm trying to convert some Go code to C#. In Go there is, in example:

type MyType interface{}

Is this something like object in C# or MyType<T>?

In Go code, there is an array of these interfaces and every one of them returns a function with different arguments.

I can return List of Funcs in C# but then the dynamic arguments are the problem.

I'm having problems about representing this in C#. Need some guidance to find the right path.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douzhuiqing1151 douzhuiqing1151 2014-01-15 16:46

    By far the best option is to just avoid this entirely. Rather than accepting both a delegate and a list of arguments that should be passed to it when invoked, just require all of the delegates to be parameterless (i.e. Action). It is then the responsibility of the caller, if they wish to have a method run with a given set of arguments, to wrap it in a parameterless method that invokes the given function with a fixed set of arguments. This can be done conveniently using a lambda in most circumstances, so this is not a burdonsome request for the caller; in fact, it's generally going to be easier on them then specifying a function and an argument list.

    Your method signature then becomes:

    public void Foo(Action action) {}

    And to the caller it'll be:

    Foo(() => SomeMethod(firstArgument, secondArgument));

    You can then make a List<Action> from those actions which you can easily invoke without messing around with reflection or dynamic typing. This solution maintains entirely compile time checking of all functions and their arguments, so you don't need to worry about dealing with invalid arguments being supplied.

    点赞 评论 复制链接分享