c# - 表达式重载作为参数
我想找到一种方法将表达式(如果可能的话进行编译)作为参数传递给函数。 该表达式将始终返回相同的类型。我想将该表达式(函数)保存为参数。
如果可能的话,一个(简短的)例子将非常感激。 :-) 我不知道是否可能。
更多信息:输入是一个表达式,其中仅应固定返回类型。我需要一种方法来定义这种原型。
方法调用示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看得很远。我只需要使用“Func”。它就像一个魅力。
这是我修正后的样本...
I was looking to far. I only have to use "Func". It works like a charm.
This is my sample corrected...
我唯一能想到的就是这样 - 不要尝试使用委托,而是定义一个接口,每个实现都会返回它想要的任何内容。
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.
我的同事奥马尔向我展示了解决问题的另一种方法......
只需使用委托...由于编译器的 lambda 解析...
Another way to solve the problem that my collegue Omar show me...
Simply with delegate... Due to lambda resolution by the compiler...