Partial classes in separate dlls

C#DllPartial Classes

C# Problem Overview


Is it possible to have two parts (same namespace, same class name) to a partial class in separate DLLs?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

No it is not possible. When the assembly is compiled the class needs to be finished.

Solution 4 - C#

While other answers do provide the unpleasant "No" that anyone landing on this page didn't want to see or hear, 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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBradView Question on Stackoverflow
Solution 1 - C#Justin NiessnerView Answer on Stackoverflow
Solution 2 - C#Adam RobinsonView Answer on Stackoverflow
Solution 3 - C#Darin DimitrovView Answer on Stackoverflow
Solution 4 - C#dotNETView Answer on Stackoverflow
Solution 5 - C#thumbmunkeysView Answer on Stackoverflow
Solution 6 - C#GlenoView Answer on Stackoverflow
Solution 7 - C#Joakim KungsmanView Answer on Stackoverflow