Is it possible to have two parts (same namespace, same class name) to a partial class in separate DLLs?
-
like the others said it is not possible, but there are ways to supplement classes with new behavior from different sources, what exactly do you want to do in different dlls? – Ozan Nov 08 '10 at 10:55
-
In my case, I want to add a couple of static methods to a class that is defined in a 3rd party library. Extension methods won't work and neither will interfaces and inheritance, because the class is static. After discovering that you can have partial static classes I came here to find out if it works across assemblies but alas it looks like this won't work either. – Ben Jun 16 '15 at 16:39
7 Answers
From MSDN -Partial Classes and Methods:
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.

- 242,243
- 40
- 408
- 536
No. Partial classes are a purely language feature. When an assembly is compiled, the files are combined to create the type. It isn't possible to spread the files out into different assemblies.
Depending on what you want to do, though, you might be able to use extension methods to accomplish what you need.

- 182,639
- 35
- 285
- 343
While other answers do provide the unpleasant "No" that anyone landing on this page didn't want to see, I was struck by another thought that hasn't been mentioned here yet. If partial classes were allowed across assemblies, one would get access to private members of existing types that were not written by him, thus allowing him to manipulate them in ways that were not intended by the original author, thus jeopardizing the functionality of all inheriting classes too.
Not only that, those classes in other assemblies (and their children) would need to be recompiled to make it work. Thus it is logically not possible to allow splitting a class over different assemblies.
Note: Read @Zar Shardan's comment below. That is another very important issue, even more important than private member access.

- 33,414
- 24
- 162
- 251
-
2One can easily mess with private members via reflection if really needed. It is not possible to split a class between assemblies because circular references are not allowed and they would be needed in order for different parts of the class to interact. – Zar Shardan May 12 '19 at 22:01
No it is not possible. When the assembly is compiled the class needs to be finished.

- 1,023,142
- 271
- 3,287
- 2,928
You can use extension methods when you want to add a method to a class in a different dll. The one drawback of this method is that you cant add static methods.

- 20,606
- 8
- 62
- 110
The question is why would you want to make a partial class in another assembly? You can define abstract classes and interfaces across assemblies, maybe you need to look into that.

- 16,621
- 12
- 64
- 85
-
11IMHO there are valid cases, for example: You might want to add MVC form annotations to a model class defined in a different assembly. The assembly itself does not know in which context it is used, so the class in the assembly should not have any annotations. They could be added in a partial class in the MVC context though (if it were possible). – nyi Mar 28 '14 at 11:11
-
4nyi, why not just inherit from that class and declare your attributes on top? You can even call it the same name, if you prefer. – Gleno Mar 28 '14 at 11:24
You probably just want to create a Wrapper class within you own library, around the class in the 3rd part library. Then add whatever functionality to the wrapper class.

- 21
- 3