C# 类不实现接口成员-但该方法已包含在类中
我有一个类和一个接口,目前只有一个方法。即使实现了该方法,我仍然会遇到以下错误: “PaymentManifestController”未实现接口成员“IPaymentManifestController.CreateNewPaymentManifest(PaymentInstruction)” 我不知道为什么——因为我已经实现了这个方法 PaymentManifestController.cs:C# 类不实现接口成员-但该方法已包含在类中,c#,inheritance,C#,Inheritance,我有一个类和一个接口,目前只有一个方法。即使实现了该方法,我仍然会遇到以下错误: “PaymentManifestController”未实现接口成员“IPaymentManifestController.CreateNewPaymentManifest(PaymentInstruction)” 我不知道为什么——因为我已经实现了这个方法 PaymentManifestController.cs: public class PaymentManifestController : IPayment
public class PaymentManifestController : IPaymentManifestController
{
public IAction CreateNewPaymentManifest(PaymentInstruction request)
{
throw new NotImplementedException();
}
}
public interface IPaymentManifestController
{
IAction CreateNewPaymentManifest(PaymentInstruction request);
}
IPaymentManifestController.cs:
public class PaymentManifestController : IPaymentManifestController
{
public IAction CreateNewPaymentManifest(PaymentInstruction request)
{
throw new NotImplementedException();
}
}
public interface IPaymentManifestController
{
IAction CreateNewPaymentManifest(PaymentInstruction request);
}
PaymentInstruction
是一个定义多个参数的对象,而IAction
是我们使用的外部库-文件所在的两个项目都引用了操作
库。混凝土类的项目也引用接口项目
向接口中添加额外的属性和方法,如void foo()
,并在类中实现它们——只是这个方法似乎不起作用
有人知道我该怎么做吗?任何帮助都将不胜感激。我想我已经找到了一个解决方案——不管怎样,这是一个适合我的解决方案,以防其他人遇到同样的问题 我没有在方法中使用类类型作为参数,而是使用了它的接口。我在接口和类中都改变了这一点。这修复了错误
public class PaymentManifestController : IPaymentManifestController
{
// Notice I have changed PaymentInstruction to IPaymentInstruction.
public IAction CreateNewPaymentManifest(IPaymentInstruction request)
{
throw new NotImplementedException();
}
}
我能想象的唯一原因是这两个文件在不同的范围内引用了
IAction
或PaymentInstruction
,因此它们不是相同的类型,因此方法签名不匹配。仔细检查您的项目是否引用了这些类型的任何副本。在不同的命名空间中是否有多个PaymentInstruction
的实现?@TomW我刚刚检查过-没有。只有一个名为PaymentInstruction的类,但它确实从自己的接口继承。没问题。@ScottHannen否-所有接口和类都共享同一名称空间。该接口必须位于类之外,以便不同类中的所有代码都可以访问该对象。