-->

Partial classes in separate dlls

2019-01-17 18:57发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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.



回答6:

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.