c# - 表达式重载作为参数

时间:2011-04-08 14:38:36

标签: c# lambda delegates overloading func

我想找到一种方法来传递一个表达式(如果可能的话编译)作为函数的参数。 表达式将始终返回相同的类型。我想将该表达式(函数)保存为参数。

如果可能的话,一个(简短的)例子会非常感激。 :-) 我不知道是否有可能。

更多信息:输入是一个表达式,其中只应修复返回类型。我需要一种方法来定义这种原型。

方法调用示例:

public delegate double[] ValueRetreiverFunc(params object[] anyNumberOfParams);

public class Class1
{
    public double[] Func()
    {
        double[] values = new double[1];
        values[0] = 2.0;
        return values;
    }
}

public class Class2
{
    public double[] Func(int n)
    {
        double[] values = new double[n];
        for (int c = 0; c < n; c++)
        {
            values[c] = 3.0;
        }
        return values;
    }
}

public class ClassTest
{
    public ValueRetreiverFunc ValueRetreiverFunc { get; set; }

    public void SetValueRetreiverFunc(ValueRetreiverFunc valueRetreiverFunc)
    {
        ValueRetreiverFunc = valueRetreiverFunc;
    }

    static void Test()
    {
        ClassTest classTest = new ClassTest();
        Class1 class1 = new Class1();
        Class2 class2 = new Class2();
        classTest.SetValueRetreiverFunc(()=> class1.Func());
        DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());

        classTest.SetValueRetreiverFunc(()=> class2.Func(7));
        DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());
    }
}

3 个答案:

答案 0 :(得分:2)

我期待远。我只需要使用“Func”。它就像一个魅力。 这是我更正的样本......

public class Class1
{
    public double[] GetValues()
    {
        double[] values = new double[1];
        values[0] = 2.0;
        return values;
    }
}

public class Class2
{
    public double[] GetValues(int n)
    {
        double[] values = new double[n];
        for (int c = 0; c < n; c++)
        {
            values[c] = 3.0;
        }
        return values;
    }
}

public class ClassTest
{
    public Func<double[]> ValueRetreiverFunc;


    public void SetValueRetreiverFunc(Func<double[]> valueRetreiverFunc)
    {
        ValueRetreiverFunc = valueRetreiverFunc;
    }

    public static void Test()
    {
        ClassTest classTest = new ClassTest();
        Class1 class1 = new Class1();
        Class2 class2 = new Class2();
        classTest.SetValueRetreiverFunc(() => class1.GetValues());
        DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());

        classTest.SetValueRetreiverFunc(() => class2.GetValues(7));
        DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());
    }

    static void DoProcessArrayOfDouble(double[] doubleArray)
    {
        foreach(double d in doubleArray)
        {
            Debug.Print(d.ToString());
        }
    }

}

答案 1 :(得分:0)

我唯一能想到的就是这样 - 而不是尝试使用委托,定义一个接口,每个实现返回它想要的任何东西。

public interface IDoubleReturningClass
{
     double[] DoSomething(params object[] anyNumberOfParams);
}

public class Class1 : IDoubleReturningClass
{
     public double[] DoSomething(params object[] anyNumberOfParams)
     {
         double[] values = new double[1];
         values[0] = 2.0;
         return values;
     }
}

public class Class2 : IDoubleReturningClass
{
     public double[] DoSomething(params object[] anyNumberOfParams)
     {
         double[] values = new double[n];
         for (int c = 0; c < n; c++)
         {
             values[c] = 3.0;
         }
         return values;
     }
}

public class ClassTest
{
      public double[] Values { get; set; }

      public void SetValues(IDoubleReturningClass item)
      {
             Values = item.DoSomething( /* Your Params */);
      }
}

答案 2 :(得分:0)

另一种解决我的同事Omar向我展示的问题的方法...... 只需使用委托...由于编译器的lambda分辨率......

public class TestDelegate
{
    public delegate double[] ValueRetreiverFunc();
    // or  public Func<double[]> ValueRetreiverFunc;

    public class Class1
    {
        public double[] Func()
        {
            double[] values = new double[1];
            values[0] = 2.0;
            return values;
        }
    }

    public class Class2
    {
        public double[] Func(int n)
        {
            double[] values = new double[n];
            for (int c = 0; c < n; c++)
            {
                values[c] = 3.0;
            }

            return values;
        }
    }

    public class ClassTest
    {
        public ValueRetreiverFunc ValueRetreiverFunc { get; set; }

        public void SetValueRetreiverFunc(ValueRetreiverFunc valueRetreiverFunc)
        {
            ValueRetreiverFunc = valueRetreiverFunc;
        }

        public static void Test()
        {
            ClassTest classTest = new ClassTest();
            Class1 class1 = new Class1();
            Class2 class2 = new Class2();

            classTest.SetValueRetreiverFunc(() => class1.Func());
            DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());
            classTest.SetValueRetreiverFunc(() => class2.Func(7));
            DoProcessArrayOfDouble(classTest.ValueRetreiverFunc());
        }

        static void DoProcessArrayOfDouble(double[] doubleArray)
        {
            foreach (double d in doubleArray)
            {
                Debug.Print(d.ToString());
            }
        }
    }
}