1
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public abstract class AttBase : Attribute
{
public static string Apply(object obj);
}
public class FooAttribute : AttBase {}
public class BarAttribute : AttBase {}
public class MyClass
{
[Foo]
[Bar]
public string MyProperty { get; set; }
}
一度に各派生属性の1つだけを使用するように開発者を制限したいと考えています。コンパイラはこの段階でコンパイルエラーを出さなければなりません。どうすればこれを達成できますか?出来ますか?あなたの目的は、単にいくつか列挙型を定義する代わりに、いくつかの使用のためのクラスにいくつかのユニークな指標を追加する場合複数の使用のために子属性を制限する
のSystem.Typeをを使用していた場合はAttBase
のプロパティとして追加知っている、それは可能ではないttributes。 'AllowMultiple = false'は特定のAttributeクラスに対してのみ定義されているので、' Foo'属性を複数使用することはできませんが、完全な継承ツリーには使用できません。 –