如何在 C# 中将特定参数标记为已过时/已弃用?
我希望能够保持 C# API 与现在相同,但只需弃用方法调用中的参数之一。是否可以这样做,或者我是否需要创建一个不带参数的新方法并将原始方法标记为已过时?
I would like to be able to keep a C# API the same as it is now, but simply deprecate one of the parameters in a method call. Is it possible to do so, or do I need to create a new method without the parameter and mark the original one as Obsolete?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用新签名创建一个新方法,并将当前方法标记为已过时。
您要不惜一切代价避免的是代码破坏!然后,特别是在公司框架中,例如,您想宣传您的方法将不再受支持,但您不想为依赖解决方案因架构或设计决策或您这边的崩溃而负责,对吗?
ObsoleteAttribute
类将执行以下操作给你的伎俩。一旦某个类成员被标记为过时,客户端、使用您框架的用户、甚至同一项目下的同事都会发出警告,以继续这种方式。
这将通告您的代码用户不再支持
MyMethod
。经过一段时间后,足够合理地允许每个人更改他/她的代码,当代码中仍然使用过时的方法时,您可以告诉此属性抛出错误消息。
通过将第二个
ObsoleteAttribute
类构造函数参数设置为true
,您可以告诉编译器将此方法的使用作为错误进行通告。一段时间后,您可以从代码中完全删除您的方法,以稍微清理一下。这是敏捷软件开发方法论鼓励的重构方法的一部分。
这有帮助吗?
You will need to create a new nethod with the new signature, and mark the current as obsolete.
What you want to avoid at all cost is a code break! Then, particularly in a company framework, you want to advertise that your method will no longer be supported, for example, but you do not want to be responsible for depending solutions to crash because of an architecture or design decision or your side, right?
The
ObsoleteAttribute
class will do the trick for you.Once a class member marked as obsolete, a warning will be raised on the client-side, the ones who use your framework, to continue that way, or even one of your colleague under the same project.
This will advertise that
MyMethod
is no longer supported throughout your code users.After a certain period of time, reasonable enough to allow everyone to change his/her code, you may tell this attribute to throw an error message when your obsolete method is still used in the code.
By setting the second
ObsoleteAttribute
class constructor parameter totrue
, you tel the compiler to advertise the use of this method as an error.After some time only, you can completely remove your method from your code to clean it up a little. This is part of the refactoring methods encouraged by the Agile Software Development methodology.
Does this help?
是的,我认为唯一的方法是创建一个不带参数的新方法,并用
ObsoleteAttribute
标记原始方法。Yes, I think the only way is to create a new method without the parameter and mark the original one with
ObsoleteAttribute
.使用 Obsolete 属性:
属性中的布尔值告诉编译器生成警告,如果将其更改为 true,编译器将生成错误。有关相关文档,请参阅此处。
With the Obsolete attribute:
The Boolean in the attribute tells the compiler to generate a warning, if you change it to true the compiler will generate an error. See here for the documentation on it.
是的。您只能将类型和类型成员标记为过时,而不能个别参数。但是,您可以重载该方法,从而保持相同的名称并将旧方法标记为已过时:
Yes. You can only mark types and members of types as obsolete, not individual parameters. You could, however, overload the method, thereby keeping the same name and mark the old one as obsolete:
我认为最好创建一种新方法。
使参数过时是什么意思?这是否取决于该参数的具体值?当你调用一个方法时,你总是必须指定参数。
I think it would be best to create a new method.
What do you mean by making the parameter obsolete? Does this depend on a specific value for that argument? When you call a method, you always have to specify the argument anyway.