20

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.

Matthew Vines
  • 27,253
  • 7
  • 76
  • 97

2 Answers2

9

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...

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • You can implement **different** interfaces in the different partial classes, and they will be aggregated into the final result. – Jonathan Dickinson Aug 19 '11 at 13:44
  • I don't 'need' a partial interface, but I want one, as it will make merging changes from the vendor back in much easier, since his file will mostly unchanged. (save the new partial declaration of course) – Matthew Vines Aug 19 '11 at 13:45
  • @Jonathan I can't really do different interfaces either, as the code uses the objects as an ICondition. If I have 2 interfaces doing the work of one interface, how do I manage the type juggling when I am consuming the objects later on? – Matthew Vines Aug 19 '11 at 13:47
  • @Matthew. I was expanding his answer with irrelevant (but interesting) information. The crux of it is: both interfaces and classes support partial declarations. It is up to you which partial class you place methods - but you only need the `: ICondition` on one of them. – Jonathan Dickinson Aug 19 '11 at 13:52
  • hmm, I may be missing something, but in my code, partial Condition class one has error Does not implement ICondition member MethodC(), and partial Condition class two has errors does not implement ICondition member MethodA() and does not implement ICondition memeber MethodB(). – Matthew Vines Aug 19 '11 at 13:52
  • Remember, effectively, the C# compiler combines partial classes into one file (it's not exactly what happens, but the end result is the same). They are NOT different types. The end-assembly has no concept of partial classes. Are both classes in the same project, and under the same namespace? – Jonathan Dickinson Aug 19 '11 at 13:53
  • @Jonathan I've amended my answer to remove that statement, I thought it was true, but I was remembering something completely different. The interface definition is only required on one side of the partial, but putting it on both doesn't cause any errors. – Adam Houldsworth Aug 19 '11 at 13:54
  • @Jonathan I feel silly, I had the 2 partial classes in different namespaces via a typo. Thanks so much for your time, you were right on track. – Matthew Vines Aug 19 '11 at 13:54
  • I just implemented the OP's original code in four separate files and it worked just fine. – FishBasketGordo Aug 19 '11 at 13:55
  • 1
    @Matthew perhaps the partial definitions are in different namespaces? I just changed on of my namespaces to try it and I get the error you describe. – Adam Houldsworth Aug 19 '11 at 13:55
  • @Adam that's exactly it, see a couple comments prior and my edit to the original question. Thanks for looking at over this with me. – Matthew Vines Aug 19 '11 at 14:07
3

Maybe your partial classes are not in the same assembly?

Otherwise, your example should just work fine...

sloth
  • 99,095
  • 21
  • 171
  • 219