[英]C# Pass a Class as a parameter
我有一个接口,有一些方法
interface IFunction
{
public double y(double x);
public double yDerivative(double x);
}
我有静态类,正在实现它。
static class TemplateFunction:IFunction
{
public static double y(double x)
{
return 0;
}
public static double yDerivative(double x)
{
return 0;
}
}
我想将这些类作为参数传递给另一个函数。
AnotherClass.callSomeFunction(TemplateFunction);
以及其他一些捕获请求的类
class AnotherClass
{
IFunction function;
public void callSomeFunction(IFunction function)
{
this.fuction = function;
}
}
好吧,它不起作用......我试图使用Type表达式,但是接缝打破了使用接口的想法。 有没有人有想法,如何纠正代码?
静态类无法实现接口,但您可以通过使类非静态和通用方法轻松克服此问题:
class AnotherClass
{
IFunction function;
public void callSomeFunction<T>()
where T: IFunction, new()
{
this.fuction = new T();
}
}
这非常接近您想要的语法:
AnotherClass.callSomeFunction<TemplateFunction>();
但是我实际上认为这种方法太复杂了,可能会使某人感到困惑,您应该遵循Servy的方法,该方法更简单:
AnotherClass.callSomeFunction(TemplateFunction.Instance);
获取静态类来实现接口的概念方法是使用单例,即使该单例不包含任何状态:
public sealed class TemplateFunction : IFunction
{
private TemplateFunction() { }
private static TemplateFunction instance = new TemplateFunction();
public static TemplateFunction Instance { get { return instance; } }
public double y(double x)
{
return 0;
}
public double yDerivative(double x)
{
return 0;
}
}
另一种选择是不使用接口,而是让您的方法接受一个或多个委托。 如果您只需要一个方法就可以,如果有两个方法就可以,有时也可以。 如果你有两个以上,这通常是一个问题。
public class AnotherClass
{
public static void callSomeFunction(Func<double, double> y
, Func<double, double> yDerivitive)
{
//store delegates for later use
}
}
AnotherClass.callSomeFunction(TemplateFunction.y, TemplateFunction.yDerivative);
您如何使用通用方法来捕获所需的类型。
像这样:
public void callSomeFunction<T>()
{
//the type is T
//you can create an instance of T with System.Activator.CreateInstance(T) and T's methods
//alternatively if the classes are static you can call the methods with reflection knowing only their name.
}
无论如何,如果你想要这样做的原因是因为你想要有多个实现相同方法的类,并且你想编写一个方法来调用基于类型的那些方法的某个实现,那么其他解决方案可能在顺序,像重载。
或者,如果这确实是你想要做的,那么请记住,传递接口将不允许你使用我提出的方法,因为Activator需要访问Type以便它可以创建一个实例。
您可以按照Allon的说明进行操作,并将TemplateFunction更改为none静态,然后执行以下操作:
var anotherClass = new AnotherClass();
var templateFunction = new TemplateFunction();
anotherClass.callSomeFunction(templateFunction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.