使用另一个接口属性的 IList 实现接口的类...如何?
我有两个这样的接口:
public interface IMyInterface1
{
string prop1 { get; set; }
string prop2 { get; set; }
}
public interface IMyInterface2
{
string prop1 { get; set; }
IList<IMyInterface1> prop2 { get; set; }
}
我定义了两个实现这些接口的类:
public class MyClass1 : IMyInterface1
{
public string prop1 {get; set;}
public string prop2 {get; set;}
}
public class MyClass2 : IMyInterface2
{
public string prop1 {get; set;}
public IList<MyClass1> prop2 {get; set;}
}
但是当我构建代码时,出现以下错误消息:
“ClassLibrary1.MyClass2”未实现接口成员“ClassLibrary1.IMyInterface2.prop2”。 'ClassLibrary1.MyClass2.prop2' 无法实现 'ClassLibrary1.IMyInterface2.prop2',因为它没有匹配的 'System.Collections.Generic.IList' 返回类型
我该如何在我的设备上实现 IMyInterface2 的“IList prop2”班级?
I have two interfaces like these:
public interface IMyInterface1
{
string prop1 { get; set; }
string prop2 { get; set; }
}
public interface IMyInterface2
{
string prop1 { get; set; }
IList<IMyInterface1> prop2 { get; set; }
}
I have defined two classes that implement the interfaces:
public class MyClass1 : IMyInterface1
{
public string prop1 {get; set;}
public string prop2 {get; set;}
}
public class MyClass2 : IMyInterface2
{
public string prop1 {get; set;}
public IList<MyClass1> prop2 {get; set;}
}
but when I build the code I have the following error message:
'ClassLibrary1.MyClass2' does not implement interface member 'ClassLibrary1.IMyInterface2.prop2'. 'ClassLibrary1.MyClass2.prop2' cannot implement 'ClassLibrary1.IMyInterface2.prop2' because it does not have the matching return type of 'System.Collections.Generic.IList'
How can I do to implement the "IList prop2" of IMyInterface2 on my class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的接口要求实现类提供
IList
类型的属性,而不是IList<实现 IMyInterface1> 的类
。如果您希望它起作用,您需要使
IMyInterface2
通用:然后
MyClass2
变为:Your interface requires that the implementing class provide a property that is of type
IList<IMyInterface1>
, notIList<class that implements IMyInterface1>
.You'll need to make
IMyInterface2
generic if you want this to work:Then
MyClass2
becomes:这是因为您的接口
MyInterface2
有一个属性,它是类型IInterface1
的通用列表,并且在实现此接口的类中,即MyClass2
您有将属性声明为MyClass1
类型的列表。要修复此问题,请将
Prop2
的类定义更改为MyInterface1
的列表,或将Prop2
的接口定义更改为MyInterface1
的列表。代码>MyClass1。例如
This is because your interface
MyInterface2
has a property which is a generic list of typeIInterface1
and in the class that implements this interface i.e.MyClass2
you have declared the property as being a list of typeMyClass1
.To fix, either change the class definition of
Prop2
to be a list ofMyInterface1
or change the interface definition ofProp2
to be a list ofMyClass1
.e.g.
我不确定我是否会将其称为昨天的问题的重复项 但是....
NET 不支持返回类型协方差。这意味着您无法从实现需要更通用类型的接口的类返回派生类型。
解决方法是显式实现接口的成员,这会给您带来麻烦:
但是,在这种情况下,如果您尝试调用
IMyInterface.prop2.Add()
,那么显式实现这样的接口将会导致问题IMyInterface.prop2
不再引用与 prop2 相同的集合。解决该问题的另一种方法是实现 Adam 的建议,并使
IMyInterface2
通用,这样您就可以提供实现IMyInterface1
的任何类型。I'm not sure if I'd call this a duplicate of the question from yesterday but...
.NET doesn't support return type covariance. That means you can't return a derived type from a class that implements an interface that requires a more generic type.
The workaround is to explicitly implement the member of the interface giving you trouble:
In this case, though, explicitly implementing the interface like that will cause issues if you try to call
IMyInterface.prop2.Add()
sinceIMyInterface.prop2
doesn't reference the same collection as prop2 anymore.The other way to solve the problem would be to implement Adam's suggestion and make
IMyInterface2
generic so you can supply any type that implementsIMyInterface1
.MyClass2
必须实现接口中声明的属性:即使
MyClass1
实现IMyInterface1
,在以下情况下也可能会导致问题:该类希望能够分配任何实现
IMyInterface1
的实例,但该类需要MyClass1
的具体实例。MyClass2
must implement the property as it's declared in the interface:Even though
MyClass1
implementsIMyInterface1
, it may cause problems in cases such as:Anyone uses the class expects to be able to assign any instance which implements
IMyInterface1
but the class expects concrete instances ofMyClass1
.尝试实现这样的代码的问题是这样的。如果编译器允许您编译该代码,那么您可以编写如下代码:
该接口应该允许您将任何 IList放入其中。到 prop2 中,但 MyClass2 无法处理它。这就是为什么您无法按照您的建议实现可写属性的原因。
The issue with trying to implement the code like that is this. If the compiler allowed you to compile that code, then you could write code like this:
The interface should allow you to put any IList<IMyInterface2> into prop2, but MyClass2 can't handle that. That's why you can't implement a writable property as you have suggested.