C#是否有一些模式/语法/库来完成类似Ruby on Rails(https://apidock.com/rails/Module/delegate)上的委托助手方法的工作?
问题是,如果您用Google搜索C#的委托人,您只会找到有关C#委托人概念的信息,该信息与rails中的概念稍有不同。
继续旅行,Javascript / Typescript是否也有等效功能?
答案 0 :(得分:2)
c#中没有等效项,您需要在每种情况下手动实现它(创建Facade方法)。
链接示例:
class Foo
CONSTANT_ARRAY = [0,1,2,3]
@@class_array = [4,5,6,7]
def initialize
@instance_array = [8,9,10,11]
end
delegate :sum, to: :CONSTANT_ARRAY
delegate :min, to: :@@class_array
delegate :max, to: :@instance_array
end
Foo.new.sum # => 6
Foo.new.min # => 4
Foo.new.max # => 11
可以翻译为c#:
public class Foo
{
public static readonly IReadOnlyList<int> CONSTANT_ARRAY = new[] {0, 1, 2, 3};
public static int[] class_array = {4, 5, 6, 7};
public int[] instance_array = {8, 9, 10, 11};
public int sum() => CONSTANT_ARRAY.Sum();
public int min() => class_array.Min();
public int max() => this.instance_array.Max();
}
...
new Foo().sum(); // => 6
new Foo().min(); // => 4
new Foo().max(); // => 11