【问题标题】:Generic class for functions with any signature c#具有任何签名 c# 的函数的泛型类
【发布时间】:2013-07-25 10:53:53
【问题描述】:

我正在尝试将 python 代码转换为 c#

  class fwrapper:
      def __init_ _(self,function,childcount,name):
          self.function=function
          self.childcount=childcount
          self.name=name
  class node:
      def __init_ _(self,fw,children):
          self.function=fw.function
          self.name=fw.name
           self.children=children
      def evaluate(self,inp):
          results=[n.evaluate(inp) for n in self.children]
          return self.function(results)

我发现在 c# 中很难实现。 1. 在 fwrapper.function 类中,它可以采用任何签名的函数。 2.获取函数的返回类型,可用于提及评估函数的返回类型

非常感谢

【问题讨论】:

    标签: c# python .net oop


    【解决方案1】:

    不完全确定你要做什么,但如果你有这样的事情:

    public class FWrapper<TChild, TResult>{
    
        private int childCount;
        private string name;
        private Func<TChild, TResult> function;
    
        public Func<TChild, TResult> Function { get { return function; } }
    
        public FWrapper(Func<TChild, TResult> Function, int ChildCount, string Name){
            this.childCount = ChildCount;
            this.name = Name;
            this.function = Function;
       }
    }
    
    public class Node<TChild, TResult>{
    
        private FWrapper<TChild, TResult> fw;
        private IEnumerable<TChild> children;
    
        public Node(FWrapper<TChild, TResult> FW, IEnumerable<TChild> Children){
            this.fw = FW;
            this.children = Children;
        }
    
        public IEnumerable<TResult> Evaluate(){
    
            var results = new List<TResult>();
    
            foreach(var c in children){
                results.Add(fw.Function(c));
            }
    
            return results;
        }
    }
    

    你现在有一个 FWrapper 类,它接受任何以 TChild 作为参数并返回一个 TResult 的函数和一个使用相同 TChild 和 TResult 类型的 Node 类,因此,例如,你可以这样做(简单的例子)

    //Generic function that takes an int and returns a string
    var func = new Func<int, string>(childInt => {
        var str = "'" + childInt.ToString() + "'";
        return str;
    });
    
    var fw = new FWrapper<int, string>(func, 10, "Foobar");
    var children = new List<int>(){ 1,2,3,4,5,6,7,8,9,10 };
    
    var node = new Node<int, string>(fw, children);
    var results = node.Evaluate();
    
    foreach(var r in results){
        Console.WriteLine(r);
    }
    
    //'1'
    //'2'
    //..
    //..
    //'9'
    //'10'
    

    【讨论】:

      【解决方案2】:

      我猜是这样的?

      class Node {
          private FWrapper fw;
          private List<Node> children;
      
          public Node(FWrapper fw, List<Node> children){
              this.fw = fw;
              this.children = children;
          }
      
          public Evaluate(Object inp){
              var results = from child in children select child.Evaluate(inp);
              return fw.function(results);
          }
      
      }
      

      这只是一个起点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 2020-09-24
        • 2016-11-09
        • 2016-01-12
        • 1970-01-01
        相关资源
        最近更新 更多