单独 dll 中的部分类
分部类是否可以在单独的 DLL 中拥有两个部分(相同的命名空间、相同的类名)?
Is it possible to have two parts (same namespace, same class name) to a partial class in separate DLLs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 MSDN - 部分类和方法:
From MSDN -Partial Classes and Methods:
不。部分类纯粹是语言功能。编译程序集时,会将文件组合起来以创建类型。不可能将文件分散到不同的程序集中。
不过,根据您想要执行的操作,您也许可以使用扩展方法< /a> 来完成你所需要的。
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.
虽然其他答案确实提供了令人不快的“不”,这是任何登陆此页面的人都不想看到的,但我对这里尚未提及的另一个想法感到震惊。如果允许跨程序集使用部分类,则人们将可以访问不是由他编写的现有类型的私有成员,从而允许他以原作者不希望的方式操作它们,从而危及所有继承类的功能也。
不仅如此,其他程序集(及其子程序集)中的那些类需要重新编译才能使其工作。因此,逻辑上不可能允许将类拆分到不同的程序集上。
注意:阅读下面@Zar Shardan 的评论。这是另一个非常重要的问题,甚至比私人会员访问权限更重要。
While other answers do provide the unpleasant "No" that anyone landing on this page didn't want to see, 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.
Note: Read @Zar Shardan's comment below. That is another very important issue, even more important than private member access.
不,这是不可能的。编译程序集后,需要完成该类。
No it is not possible. When the assembly is compiled the class needs to be finished.
当您想要将方法添加到不同 dll 中的类时,可以使用扩展方法。
此方法的一个缺点是无法添加静态方法。
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.
问题是为什么你想在另一个程序集中创建一个分部类?您可以跨程序集定义抽象类和接口,也许您需要研究一下。
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.
您可能只想在您自己的库中围绕第三部分库中的类创建一个包装器类。然后向包装类添加任何功能。
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.