3

Possible Duplicate:
Is it possible to declare a partial class in two projects

Say In my project solution, I have two project, ProjectA and ProjectB. ProjectA created: MyClassA

Then ProjectB has ProjectA referenced. Is it possible to create a partial class for ProjectA's MyClassA in ProjectB?

My Project A's MyClassA:

namespace TestPartial
{
    public class MyClassA
    {
        public string MyName { get; set; }
    }
}

My Project B's MyClassB:

namespace TestPartial
{
    public partial class MyClassA
    {
        public DateTime BirthDate { get; set; }
    }

}

But obviously both properties doesn't merge.... So I guess is only work if they are in same project only? Or there is some workaround for it?

Community
  • 1
  • 1
King Chan
  • 4,212
  • 10
  • 46
  • 78

4 Answers4

7

No, partial classes cannot span multiple project/assemblies.

From MSDN Partial Class Definitions - Restrictions :

All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • To expand; partial classes are more an IDE feature than a runtime feature, telling the compiler that multiple source code files combine to make one actual class. That class still has to be in a single location in the binaries. – KeithS Aug 17 '12 at 21:22
  • Bah, now make sense. Oh well, now I have to restructure my project a little bit. Thanks :) – King Chan Aug 17 '12 at 23:37
3

Is it possible to create a partial class for ProjectA's MyClassA in ProjectB?

No. Partial classes are just a language level feature to combine multiple source files to create a single output class. It's not something the CLR is aware of, and you can't split a class between assemblies.

If you can give more information about what you're trying to achieve, we may be able to recommend alternatives. You might want to use composition, or possibly inheritance.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

No, a partial class must be fully defined within the same assembly (dll or exe). You can't have a partial class that spans two assemblies.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
0

It isn't according to Jon Skeet, which probably means it isn't.

See: Should you use a partial class across projects?

Community
  • 1
  • 1
SDK
  • 820
  • 1
  • 12
  • 24