通常,当您创建自定义 Attribute 类时,任何具有 getter 和 setter 的公共属性都会自动公开给在该类上应用该属性的人,以便使用该属性的人可以指定该属性的值。
我想要一个自定义属性类,它公开特定属性的 getter 和 setter,但不允许在创建时将此属性指定为属性的命名参数。例子:
[AttributeUsage(AttributeTargets.Class)]
class MyCustomAttribute : Attribute
{
public bool MyProperty
{
get { /* do something */ }
set { /* do something */ }
}
}
// The following line should be a compiler error, MyProperty should
// be hidden for attribute initialization!
[MyCustomAttribute(MyProperty=true)]
class MyClass
{
};
有什么办法可以做到这一点?