单独 dll 中的部分类

发布于 2024-09-26 09:40:16 字数 46 浏览 2 评论 0原文

分部类是否可以在单独的 DLL 中拥有两个部分(相同的命名空间、相同的类名)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你的心境我的脸 2024-10-03 09:40:16

来自 MSDN - 部分类和方法

所有部分类型定义都旨在
是同一类型的零件必须是
在同一程序集中定义并且
相同的模块(.exe 或 .dll 文件)。
部分定义不能跨越
多个模块。

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.

柠栀 2024-10-03 09:40:16

不。部分类纯粹是语言功能。编译程序集时,会将文件组合起来以创建类型。不可能将文件分散到不同的程序集中。

不过,根据您想要执行的操作,您也许可以使用扩展方法< /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.

很快妥协 2024-10-03 09:40:16

虽然其他答案确实提供了令人不快的“不”,这是任何登陆此页面的人都不想看到的,但我对这里尚未提及的另一个想法感到震惊。如果允许跨程序集使用部分类,则人们将可以访问不是由他编写的现有类型的私有成员,从而允许他以原作者不希望的方式操作它们,从而危及所有继承类的功能也。

不仅如此,其他程序集(及其子程序集)中的那些类需要重新编译才能使其工作。因此,逻辑上不可能允许将类拆分到不同的程序集上。

注意:阅读下面@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.

负佳期 2024-10-03 09:40:16

不,这是不可能的。编译程序集后,需要完成该类。

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

呢古 2024-10-03 09:40:16

当您想要将方法添加到不同 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.

不如归去 2024-10-03 09:40:16

问题是为什么你想在另一个程序集中创建一个分部类?您可以跨程序集定义抽象类和接口,也许您需要研究一下。

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.

望笑 2024-10-03 09:40:16

您可能只想在您自己的库中围绕第三部分库中的类创建一个包装器类。然后向包装类添加任何功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文