Is it possible to declare a partial class in two projects

半腔热情 提交于 2019-12-01 15:09:39

问题


Consider we create a partial class in Project1 and we have a Project2 that has reference to Project1 .How is it possible to declare some other method of partial class in Project2 ?

thanks


回答1:


The partial construct is only a compiler functionality, to allow a class to be spread out in several source files. The compiled class still lives in one and only one class library (dll file).

There are two ways to extend a class in another library:

  • Inheritance, unless the class is sealed. This requires that the calling code handles all object instantiation to instantiate the new derived class.
  • Extension methods, which makes the code look like there are new methods on that class, but that is just syntactic sugar. It won't change the class itself.



回答2:


It is not possible to extend a partial class in another project. Partial is only compiler sugar. The compiler will create only one class in the resulting assembly.




回答3:


You can use the Extension methods that allow you to create additional methods for existing class




回答4:


Partial classes cannot exist out side assembly boundaries!



来源:https://stackoverflow.com/questions/8384084/is-it-possible-to-declare-a-partial-class-in-two-projects

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