jit*_*itm 4 .net c# attributes
我已经创建了属性
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
[Serializable]
public class TestPropertyAttribute : System.Attribute
{
public string Name
{
get { return _name; }
set { _name = value; }
}string _name;
}
Run Code Online (Sandbox Code Playgroud)
我应该将"名称"标记为此属性的强制属性.怎么做?
Jon*_*eet 11
将它放在构造函数中而不是作为单独的属性:
[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; } }
}
Run Code Online (Sandbox Code Playgroud)
我不相信你可以强制它并Name=...在应用属性时使用语法.