Partial Classes across Projects

烈酒焚心 提交于 2019-12-01 00:49:14

问题


Is it possible to have partial classes across projects.

e.g. Project 1 has a Customer Class. Project2 which is an optional module adds to the customer class by attaching an order class, and utilising the original Customer Class.


回答1:


You cannot use the partial keyword to split the code for a class between projects. The partial keyword is a compiler trick; the compiler will output one single class out of the parts it finds, so all parts of the class must exist with the same binary file. Once the class is compiled, there is no trace left of it being a partial class.

If you want to extend an existing class you will either need to inherit it (if it is not sealed), or create your own new class that contains the classes that you wish to combine information from.




回答2:


No. A partial class must be compiled within the same context, i.e. assembly.

What you probably want to do is use Inheritance.




回答3:


partial classes are entirely a compiler construct - it basically concatenates the class definitions together, then compiles that. There is no concept of a 'partial class' in .net, so you cannot split partial classes across different compilation units (projects or assemblies)




回答4:


you can't use partial class in a separate projects.Period you can however use extensions Class Extensions or inheritance or interface



来源:https://stackoverflow.com/questions/2132323/partial-classes-across-projects

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