使用另一个接口属性的 IList 实现接口的类...如何?

发布于 2024-09-18 13:43:56 字数 835 浏览 6 评论 0原文

我有两个这样的接口:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你另情深 2024-09-25 13:43:56

您的接口要求实现类提供 IList 类型的属性,而不是 IList<实现 IMyInterface1> 的类

如果您希望它起作用,您需要使 IMyInterface2 通用:

public interface IMyInterface2<T> where T : IMyInterface1
{
    string prop1 { get; set; }
    IList<T> prop2 { get; set; }
}

然后 MyClass2 变为:

public class MyClass2 : IMyInterface2<MyClass1>
{
     public string prop1 {get; set;}
     public IList<MyClass1> prop2 {get; set;}
}

Your interface requires that the implementing class provide a property that is of type IList<IMyInterface1>, not IList<class that implements IMyInterface1>.

You'll need to make IMyInterface2 generic if you want this to work:

public interface IMyInterface2<T> where T : IMyInterface1
{
    string prop1 { get; set; }
    IList<T> prop2 { get; set; }
}

Then MyClass2 becomes:

public class MyClass2 : IMyInterface2<MyClass1>
{
     public string prop1 {get; set;}
     public IList<MyClass1> prop2 {get; set;}
}
药祭#氼 2024-09-25 13:43:56

这是因为您的接口 MyInterface2 有一个属性,它是类型 IInterface1 的通用列表,并且在实现此接口的类中,即 MyClass2 您有将属性声明为 MyClass1 类型的列表。

要修复此问题,请将 Prop2 的类定义更改为 MyInterface1 的列表,或将 Prop2 的接口定义更改为 MyInterface1 的列表。代码>MyClass1。

例如

public interface MyInterface2
{
    public IList<MyInterface1> Prop2 { get; set; }
}

public class MyClass2 : MyInterface2
{
    public IList<MyInterface1> Prop2 { get; set; }
}

This is because your interface MyInterface2 has a property which is a generic list of type IInterface1 and in the class that implements this interface i.e. MyClass2 you have declared the property as being a list of type MyClass1.

To fix, either change the class definition of Prop2 to be a list of MyInterface1 or change the interface definition of Prop2 to be a list of MyClass1.

e.g.

public interface MyInterface2
{
    public IList<MyInterface1> Prop2 { get; set; }
}

public class MyClass2 : MyInterface2
{
    public IList<MyInterface1> Prop2 { get; set; }
}
爱的那么颓废 2024-09-25 13:43:56

我不确定我是否会将其称为昨天的问题的重复项 但是....

NET 不支持返回类型协方差。这意味着您无法从实现需要更通用类型的接口的类返回派生类型。

解决方法是显式实现接口的成员,这会给您带来麻烦:

public class MyClass2 : IMyInterface2
{
    public string prop1 { get; set; }
    public IList<MyClass1> prop2 { get; set; }
    public IList<IMyInterface1> IMyInterface2.prop2
    {
        get { return prop2.Cast<IMyInterface1>.ToList(); }
        set { prop2 = value.Cast<MyClass1>().ToList(); }
    }
}

但是,在这种情况下,如果您尝试调用 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:

public class MyClass2 : IMyInterface2
{
    public string prop1 { get; set; }
    public IList<MyClass1> prop2 { get; set; }
    public IList<IMyInterface1> IMyInterface2.prop2
    {
        get { return prop2.Cast<IMyInterface1>.ToList(); }
        set { prop2 = value.Cast<MyClass1>().ToList(); }
    }
}

In this case, though, explicitly implementing the interface like that will cause issues if you try to call IMyInterface.prop2.Add() since IMyInterface.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 implements IMyInterface1.

情释 2024-09-25 13:43:56

MyClass2 必须实现接口中声明的属性:

public class MyClass2 : IMyInterface2
{
     public string prop1 {get; set;}
     public IList<IMyInterface1> prop2 {get; set;}
}

即使 MyClass1 实现 IMyInterface1,在以下情况下也可能会导致问题

IMyInterface2 myInterface2 = new MyClass2();
myInterface2.prop2.Add(new OtherImplementationOfIMyInterface1());

:该类希望能够分配任何实现 IMyInterface1 的实例,但该类需要 MyClass1 的具体实例。

MyClass2 must implement the property as it's declared in the interface:

public class MyClass2 : IMyInterface2
{
     public string prop1 {get; set;}
     public IList<IMyInterface1> prop2 {get; set;}
}

Even though MyClass1 implements IMyInterface1, it may cause problems in cases such as:

IMyInterface2 myInterface2 = new MyClass2();
myInterface2.prop2.Add(new OtherImplementationOfIMyInterface1());

Anyone uses the class expects to be able to assign any instance which implements IMyInterface1 but the class expects concrete instances of MyClass1.

下壹個目標 2024-09-25 13:43:56

尝试实现这样的代码的问题是这样的。如果编译器允许您编译该代码,那么您可以编写如下代码:

public class MyClass3 : IMyInterface1 
{ 
    public string prop1 {get; set;} 
    public string prop2 {get; set;} 
} 

...

MyClass2 mc2 = new MyClass2();
IMyInterface2 mi2 = mc2;
mi2.prop2 = new List<MyClass3>();

该接口应该允许您将任何 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:

public class MyClass3 : IMyInterface1 
{ 
    public string prop1 {get; set;} 
    public string prop2 {get; set;} 
} 

...

MyClass2 mc2 = new MyClass2();
IMyInterface2 mi2 = mc2;
mi2.prop2 = new List<MyClass3>();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文