gpt4 book ai didi

c# - 忽略 ObsoleteAttribute 编译器错误

转载 作者:太空狗 更新时间:2023-10-30 00:59:26 27 4
gpt4 key购买 nike

我有一个用以下属性标记的枚举值。第二个参数指示编译器在使用该值时出错。我希望实现我的库的任何人都具有这种行为,但我需要在我的库中使用此枚举值。我如何告诉编译器忽略我的库中的几次使用的 Obsolete 错误。

public enum Choices
{
One,
Two,
[ObsoleteAttribute("don't use me", true)]
Three,
Four
}

解决方法(谢谢大家)

public class EnumHack
{
static EnumHack()
{
// Safety check
if (Choices!= (Choices)Enum.Parse(typeof(Choices), "Three"))
throw new Exception("Choices.Three != 3; Who changed my Enum!");
}

[Obsolete("Backwards compatible Choices.Three", false)]
public const Choices ChoicesThree = (Choices)3;
}

最佳答案

像这样在某处私有(private)一个单独的常量:

private const Choices BackwardsCompatibleThree = (Choices) 3;

请注意,任何其他人都可以做同样的事情。

关于c# - 忽略 ObsoleteAttribute 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/344630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com