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

发布于 2024-10-31 02:30:53 字数 1311 浏览 0 评论 0原文

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

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

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

方法调用示例:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生寂 2024-11-07 02:30:53

我看得很远。我只需要使用“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());
        }
    }

}

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

}
や三分注定 2024-11-07 02:30:53

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

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

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 */);
      }
}
闻呓 2024-11-07 02:30:53

我的同事奥马尔向我展示了解决问题的另一种方法......
只需使用委托...由于编译器的 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());
            }
        }
    }
}

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());
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文