简体   繁体   English

使用集合和泛型实现接口

[英]Implementing Interfaces with Collections and Generics

I am trying to do some abstraction in classes generated by entity framework class and have a setup like the following: 我试图在实体框架类生成的类中进行一些抽象,并具有如下设置:

EntityClassA (Generated)
{
   EntityCollection<EntityClassB> EntityClassBs;
}

EntityClassB (Generated)
{
   (...)
}

Partial EntityClassA : InterfaceA
{
   (...)
}


Partial ClassB : InterfaceB
{
   (...)
}

InterfaceA
{
   IEnumerable<InterfaceB> EntityClassBs;
}

but I keep getting issues saying EntityClassA does not implement properly because the return types don't match on EntityClassBs. 但我不断收到问题,说EntityClassA没有正确实现,因为返回类型与EntityClassBs不匹配。

UPDATE: My apologies, I did not intend to submit this question in this state. 更新:道歉,我不打算在这个状态下提交这个问题。 Updated example to include proper interfaceA property name and more detailed explanation. 更新示例以包含正确的interfaceA属性名称和更详细的说明。 Keep in mind this is only an example and the nomenclature does not represent actual names. 请记住,这只是一个例子,命名法并不代表实际名称。

What I am trying to do is I have a class library of wpf controls and a library for data. 我想要做的是我有一个wpf控件的类库和一个数据库。 The WPF library references the Data library for one class that it uses to construct a custom table. WPF库引用它用于构造自定义表的一个类的数据库。 So what I was trying to do was take the reliance of the data package by using Interfaces. 所以我试图通过使用Interfaces来依赖数据包。 Is there a way I can proceed like this or is there a more recommended way? 有没有办法可以像这样进行,还是有更推荐的方法?

What I am seeing is that I need to match the signature of the interface property exactly and I cannot implement interfaces like that. 我所看到的是我需要完全匹配接口属性的签名,我不能实现这样的接口。

You don't ask a question, nor do you give any details about what return types you're using, so based on your statement I'll pretend you're trying to do this: 你没有问一个问题,也没有提供你正在使用的返回类型的任何细节,所以根据你的陈述,我会假装你试图这样做:

Partial EntityClassA : InterfaceA
{
   IEnumerable<ClassB> IClassBs {get; set;}
}

Partial ClassB : InterfaceB
{
   (...)
}

InterfaceA
{
   IEnumerable<InterfaceB> IClassBs;
}

which is not valid. 这是无效的。 The InterfaceA interface specifies that IClassBs (poorly named property, by the way) returns a collection if InterfaceB s. InterfaceA接口指定IClassBs (顺便说IClassBs命名不佳的属性)返回一个集合,如果是InterfaceB的话。 Changing the method signature to return a collection of ClassB s does not match the interface definition. 更改方法签名以返回ClassB的集合与接口定义不匹配。

Note that you can return an actual list of ClassB s without changing the return type: 请注意,您可以在不更改返回类型的情况下返回ClassB的实际列表:

Partial EntityClassA : InterfaceA
{
   IEnumerable<InterfaceB> IClassBs 
   {
       get
       {
           return new List<InterfaceB>() {new ClassB()};
       }
   }
}

I think I understand what you are trying to do. 我想我明白你要做什么。 Perhaps instead of altering the signature (Can't do) you can try to add another property that just returns the same property but with the signature you looking for. 也许您可以尝试添加另一个只返回相同属性但具有您要查找的签名的属性,而不是更改签名(无法执行)。

Partial EntityClassA : InterfaceA
{
    IEnumerable<InterfaceB> CollectionEntityClassBs
    {
        get{ return (some cast or somthin)EntityClassBs;  }
    }
}

InterfaceA
{
   IEnumerable<InterfaceB> CollectionEntityClassBs;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM