gpt4 book ai didi

C# 属性强制属性

转载 作者:太空狗 更新时间:2023-10-30 00:17:07 25 4
gpt4 key购买 nike

我创建了这样的属性

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
[Serializable]
public class TestPropertyAttribute : System.Attribute
{
public string Name
{
get { return _name; }
set { _name = value; }
}string _name;
}

我应该将“名称”标记为该属性的强制属性。怎么做?

最佳答案

将它放在构造函数中而不是仅仅作为一个单独的属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
[Serializable]
public class TestPropertyAttribute : System.Attribute
{
readonly string _name;

public TestPropertyAttribute(string name)
{
_name = name;
}

public string Name { get { return _name; } }
}

我不相信您可以将其设为强制性并且在应用属性时使用 Name=... 语法。

关于C# 属性强制属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3607087/

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