如何在 C# 中将特定参数标记为已过时/已弃用?

发布于 2024-09-28 22:48:00 字数 84 浏览 11 评论 0原文

我希望能够保持 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 技术交流群。

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

发布评论

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

评论(5

横笛休吹塞上声 2024-10-05 22:48:00

简短回答:

您需要使用新签名创建一个新方法,并将当前方法标记为已过时。

更长的答案

您要不惜一切代价避免的是代码破坏!然后,特别是在公司框架中,例如,您想宣传您的方法将不再受支持,但您不想为依赖解决方案因架构或设计决策或您这边的崩溃而负责,对吗?

ObsoleteAttribute 类将执行以下操作给你的伎俩。

一旦某个类成员被标记为过时,客户端、使用您框架的用户、甚至同一项目下的同事都会发出警告,以继续这种方式。

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.")]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

这将通告您的代码用户不再支持 MyMethod

经过一段时间后,足够合理地允许每个人更改他/她的代码,当代码中仍然使用过时的方法时,您可以告诉此属性抛出错误消息。

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.", true)]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

通过将第二个 ObsoleteAttribute 类构造函数参数设置为 true,您可以告诉编译器将此方法的使用作为错误进行通告。

一段时间后,您可以从代码中完全删除您的方法,以稍微清理一下。这是敏捷软件开发方法论鼓励的重构方法的一部分。

这有帮助吗?

Short answer:

You will need to create a new nethod with the new signature, and mark the current as obsolete.

Longer answer

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.

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.")]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

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.

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.", true)]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

By setting the second ObsoleteAttribute class constructor parameter to true, 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?

執念 2024-10-05 22:48:00

是的,我认为唯一的方法是创建一个不带参数的新方法,并用 ObsoleteAttribute 标记原始方法。

Yes, I think the only way is to create a new method without the parameter and mark the original one with ObsoleteAttribute.

南汐寒笙箫 2024-10-05 22:48:00

使用 Obsolete 属性:

[Obsolete("Please use void AnotherMethod() instead", false)]
void SomeMethod(Int32 data){
}

属性中的布尔值告诉编译器生成警告,如果将其更改为 true,编译器将生成错误。有关相关文档,请参阅此处

With the Obsolete attribute:

[Obsolete("Please use void AnotherMethod() instead", false)]
void SomeMethod(Int32 data){
}

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.

天暗了我发光 2024-10-05 22:48:00

是的。您只能将类型和类型成员标记为过时,而不能个别参数。但是,您可以重载该方法,从而保持相同的名称并将旧方法标记为已过时:

class Act
{
    [Obsolete("DoSomething(int, int) is obsolete", false /*warn only*/)]
    public void DoSomething(int i, int j)
    {
    }

    public void DoSomething(int i)
    {
    }
}

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:

class Act
{
    [Obsolete("DoSomething(int, int) is obsolete", false /*warn only*/)]
    public void DoSomething(int i, int j)
    {
    }

    public void DoSomething(int i)
    {
    }
}
自由如风 2024-10-05 22:48:00

我认为最好创建一种新方法。

使参数过时是什么意思?这是否取决于该参数的具体值?当你调用一个方法时,你总是必须指定参数。

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.

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