简体   繁体   English

局部类扩展多方法类

[英]Partial class extending multiple method class

Hi so i have one interface with 2 methods which is implemented by an abstract class and one partial class in 2 separate file which extends the abstract class 嗨,我有一个接口,其中包含2个方法,这些方法由一个抽象类和一个局部类在2个单独的文件中实现,该文件扩展了该抽象类

Ex: 例如:

public inteface Iterface
{
    void A();
    void B();
}

public abstract class Abstract : Interface
{
    public abstract A();
    public abstract B();
}


// Partial1.cs    
public partial class Partial : Abstract
{
    public override A(){}
}

// Partial2.cs
public partial class Partial : Abstract
{
    public override B(){}
}

I get the error that Partial does not implement Abstract correctly, how come? 我收到Partial不能正确实现Abstract的错误,怎么来? If i would add to Partial1.cs the B() so that i would have 如果我将B()添加到Partial1.cs中,那么我将拥有

// Partial1.cs
public partial class Partial : Abstract
{
     public override A(){}
     public override B(){}
}

all good, but why i am not allowed to implement the Abstract method in whatever file containing the Partial i want? 一切都很好,但是为什么我不允许在包含我想要的Partial的任何文件中实现Abstract方法?

I've tried also with the following 我也尝试了以下

// Partial1.cs    
public partial class Partial : Abstract
{
     public override A(){}
}

// Partial2.cs
public partial class Partial
{
    public override B(){}
}

Initially i wanted just to use the Interface but i had the same error "Partial class does not implement Interface correctly" so i though that maybe i cannot do so. 最初,我只想使用Interface,但是我遇到了同样的错误“ Partial class无法正确实现Interface”,所以我虽然可能无法这样做。

Is there any way of doing what i'm trying here? 有什么办法可以做我在这里尝试的吗? Implement the methods in whichever Partial class file i want ? 在我想要的部分类文件中实现方法?

Thank you 谢谢

Short Answer: 简短答案:

It depends on your compiler/IDE. 这取决于您的编译器/ IDE。
For example, it is possible in some versions of Visual Studio you may not be able to break up an interface across multiple partial classes. 例如,在某些版本的Visual Studio中,您可能无法分解多个子类之间的接口。


You can do it. 你能行的。

In LINQPad and VS2017 this will compile just fine... 在LINQPad和VS2017中,这可以正常编译...

public interface I1
{
    void A();
    void B();
}

partial class Foo : I1
{
    public void A() {}
}

partial class Foo : I1
{
    public void B() {}
}

void Main()
{
    Foo foo = new Foo();
    foo.A();
    foo.B();
}

Consider using an alternative design 考虑使用替代设计

The alternative and viable use-case for partial classes would be to break down your class against different multiple interfaces. 部分类的另一种可行的用例是将您的类分解为不同的多个接口。 For example: 例如:

Interfaces 介面

public inteface I1
{
    void A();
}

public inteface I2
{
    void B();
}

Breaking up a class 1-1 with Interfaces 用接口分解1-1类

public partial class Partial : I1
{
    public void A() { }
}

public partial class Partial : I2
{
    public void B() { } 
}

Which ultimately would compile to 最终将编译为

public partial class Partial : I1, I2
{
    public void A() { }
    public void B() { } 
}

Found reason why it did not worked. 找到了为什么它不起作用的原因。

A partial class defined in assembly abc is not the same as defined in assembly ab 程序集abc中定义的部分类与程序集ab中定义的部分类不同

So i did not care to look about assembly info, in order for my partial class to have worked as i wanted i had to add them in the same assembly. 因此,我不在乎查看程序集信息,以便我的部分类能够按我希望的方式工作,因此我必须将它们添加到同一程序集中。

D'oh! 天哪!

EX: 例如:

namespace A.BL.Repository.ARepo 

should be used wherever the partial class is present 应该在存在局部类的任何地方使用

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

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