C#将类作为参数传递
我有一个接口,它有一些方法C#将类作为参数传递,c#,class,methods,interface,parameters,C#,Class,Methods,Interface,Parameters,我有一个接口,它有一些方法 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 sta
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;
}
}
嗯,它不起作用。。。我曾尝试使用类型表达式,但这有助于打破使用接口的想法。有人知道如何更正代码吗 使用泛型方法来捕获所调用的类型怎么样 像这样:
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.
}
public void callSomeFunction()
{
//类型是T
//您可以使用System.Activator.CreateInstance(T)和T的方法创建T的实例
//或者,如果类是静态的,您可以调用反射只知道名称的方法。
}
不管怎么说,如果你想这样做的原因是因为你想有多个类实现相同的方法,你想写一个方法,根据类型调用这些方法的特定实现,那么其他的解决方案也可能是这样的,比如重载
或者,如果这确实是您想要做的,那么请记住,传递接口将不允许您使用我向您介绍的方法,因为激活器需要访问该类型才能创建实例。静态类无法实现接口,但是,通过使类成为非静态和通用方法,您可以轻松克服这一问题:
class AnotherClass
{
IFunction function;
public void callSomeFunction<T>()
where T: IFunction, new()
{
this.fuction = new T();
}
}
让静态类实现接口的概念性方法是使用单例,即使该单例不包含任何状态:
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);
公共类另一类
{
公共静态void callSomeFunction(Func y
,函数(驱动)
{
//存储代理以供以后使用
}
}
另一个类.callSomeFunction(TemplateFunction.y,TemplateFunction.ydevirative);
您可以按照Allon所说的操作,将TemplateFunction更改为none static,然后执行以下操作:
var anotherClass = new AnotherClass();
var templateFunction = new TemplateFunction();
anotherClass.callSomeFunction(templateFunction);
静态类无法实现接口。什么不起作用?您需要提供更多关于您遇到的问题的信息。您得到的错误是什么?@AllonGuralnek我可以将接口更改为抽象静态类,然后在静态类中实现它吗?或者在C中也不可能实现它吗?@AlexMendez,显然,对于方法,Ermintar得到接口不能有
public
的错误。。。不确定是否达到了关于“静态无法实现接口”的第二个问题…谢谢,这是一个很好的解决方案。从未听说过这样的“where T:iffunction,new()”有趣的实现。:=)
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);
var anotherClass = new AnotherClass();
var templateFunction = new TemplateFunction();
anotherClass.callSomeFunction(templateFunction);