向现有 C# 类添加属性而不丢失现有扩展方法?

发布于 2024-12-06 05:00:34 字数 192 浏览 6 评论 0原文

我有一个现有的分部类,在一个项目中应用了扩展方法。

我想在不同的项目中向该类添加属性,但是当我创建第二个部分类时,扩展方法消失了。

最初,我创建了带有新属性的类作为原始类的子类,但我想避免将原始类的实例繁琐地向上转换为新的子类(尽管这可能是“最好”的方式)结束)。

是否有办法在不丢失扩展方法、不使用继承的情况下添加属性?

I have an existing partial class that has extension methods applied to it, in one project.

I want to add an attribute to that class within a different project, but when I create a second partial class the extension methods disappear.

Initially I created the class with the new attribute as a child of the original class, but I want to avoid the tedious up-cast of an instance of the original class to the new child class (though this may be the "best" way in the end).

Is there anyway to add the attribute without losing the extension methods, without using inheritance?

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

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

发布评论

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

评论(1

以酷 2024-12-13 05:00:34

您不能跨项目声明分部类 - 它必须位于单个项目中。

基本上,如果您需要类上的额外属性,则必须将其放入原始项目中。

You can't declare a partial class across projects - it's got to be in a single project.

Basically if you need an extra attribute on the class, you'll have to put that in the original project.

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