开发者

c# - Expression overload as parameter

I'd like to find a way to pass an expression (compiled if possible) as an argument to a function. The expression will always return the same type. I want to save that expression(function) as a parameter.

A (short) example would be really appreciate if possible. :-) I don't know if it is possible.

More info: Input is an expression where only the return type should be fixed. I need a way to define this kind of prototype.

Method call example:

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

public class Class1
{
    public double[] Func()
    {
        double[] values = new double[1];
        values[0]开发者_如何学Go = 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());
    }
}


I was looking to far. I only have to use "Func". It works like a charm. This is my sample corrected...

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());
        }
    }

}


The only thing I can think of is like so - instead of trying to use a delegate, define an interface and each implementation returns whatever it wants.

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 */);
      }
}


Another way to solve the problem that my collegue Omar show me... Simply with delegate... Due to lambda resolution by the compiler...

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());
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜