如何删除文本属性中的冗余

本文关键字:属性 冗余 文本 何删除 删除 | 更新日期: 2023-09-27 18:27:35

假设我有一段代码,例如:

[MyAttribute("Long long text 1", "Some more long text 1")]
[MyAttribute("Long long text 2", "Some more long text 2")]
[MyAttribute("Long long text 3", "Some more long text 3")]
public class TestClass 
{
   [...]
}

有没有办法引入常量来替换这些属性中的公共子字符串(即本例中的Long long textSome more long text)?我知道这在实际的"const"方面可能是不可能的,但肯定还有其他功能吗?

如何删除文本属性中的冗余

您可以使用常量:

public class SomeClass
{
    public const string SomeConstant = "Long long text 1";
}
[MyAttribute(SomeClass.SomeConstant)]
public class SomeOtherClass
{
}

你只需要正确地引用它们。