Partial classes in separate dlls

社会主义新天地 提交于 2019-11-27 01:43:24

问题


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:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/3858649/partial-classes-in-separate-dlls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!