Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to break an interface into 2 partial interfaces and implement it in 2 partial classes?

I am making fairly extensive and ongoing modifications to a third party product for my employer. One of the major considerations when implementing my code has been to segregate it as much as possible to make the integration of changes from the vendor to be as painless as possible. Thus far, one of the most useful tools to accomplish this has been the partial class. Using partial classes I am able to keep any new methods that I have to implement in their own file.

But today I hit a hiccup that I need to address some how. Let's say I need to extend the following interface.

public partial interface ICondition
{
  void MethodA();
  void MethodB();
}

by making the interface a partial and adding a file called ICondition.CompanyName.cs

public partial interface ICondition
{
  void MethodC();
}

now what I want to do is to implement the interface in the same way I declared it, with MethodA() and MethodB() implemented in one file of a partial class, and MethodC() in another.

public partial class Condition : ICondition
{
   public void MethodA(){ }
   public void MethodB(){ }
}

and my class Condition.CompanyName.cs

public partial class Condition : ICondition
{
   public void MethodC() { }
}

This of course doesn't work, as the interface must be implemented all in the same file. What I am hoping is there is some declaration, or bit of syntax magic that I am unaware of that will allow me to do what I want to do, and keep these methods in separate files, but the same interface.

Edit

This does in fact work, my issue was that I had my 2 partial classes in different namespaces due to a silly typo. I got started a little too early this morning I suppose, thanks everyone for your time and attention.

like image 838
Matthew Vines Avatar asked Aug 19 '11 13:08

Matthew Vines


People also ask

Can a class implement an interface partially?

Introduction. A class does not have to provide implementations for all methods. A class can provide partial implementations of the implemented interfaces. If a class does not provide the full implementation of interfaces, it must be declared abstract.

Can different parts of a partial class inherit from different interfaces?

All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations.

What is partial interface?

In C#, you can split the implementation of an interface into multiple files using the partial keyword. The partial keyword indicates that other parts of the interface can be defined anywhere in the namespace.

What is the purpose of partial class in C#?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.


2 Answers

You don't need to have a partial interface in this instance, you should still be able to put the methods where you want.

This of course doesn't work, as the interface must be implemented all in the same file

I don't see how your example doesn't work, I just did the following:

partial interface IFoo
{
    void One();
    void Two();
}

partial class Class1 : IFoo
{
    public void Two()
    { }

    public void Three()
    { }
}

In one file.

partial interface IFoo
{
    void Three();
}

partial class Class1 : IFoo
{
    public void One()
    { }
}

In another...

like image 86
Adam Houldsworth Avatar answered Oct 06 '22 01:10

Adam Houldsworth


Maybe your partial classes are not in the same assembly?

Otherwise, your example should just work fine...

like image 34
sloth Avatar answered Oct 05 '22 23:10

sloth