向现有 C# 类添加属性而不丢失现有扩展方法?
我有一个现有的分部类,在一个项目中应用了扩展方法。
我想在不同的项目中向该类添加属性,但是当我创建第二个部分类时,扩展方法消失了。
最初,我创建了带有新属性的类作为原始类的子类,但我想避免将原始类的实例繁琐地向上转换为新的子类(尽管这可能是“最好”的方式)结束)。
是否有办法在不丢失扩展方法、不使用继承的情况下添加属性?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能跨项目声明分部类 - 它必须位于单个项目中。
基本上,如果您需要类上的额外属性,则必须将其放入原始项目中。
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.