2009-10-12 4 views
5

다음은 컴파일러에서 AllowMultiple = false로 설정된 기본 특성에서 파생 된 여러 특성을 허용하지 않을 것이라고 가정합니다. 실제로 문제없이 컴파일됩니다. 여기서 무엇을 놓치고 있습니까?파생 된 특성 형식에 대한 AttributeUsage 적용

using System; 

[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=true)] 
abstract class BaseAttribute : Attribute { } 

sealed class DerivedAttributeA : BaseAttribute { } 

sealed class DerivedAttributeB : BaseAttribute { } 

    class Sample1 
    { 
     [DerivedAttributeA()] 
     [DerivedAttributeB()] 
     public string PropertyA{ get; set; } // allowed, concrete classes differ 

     [DerivedAttributeA()] 
     [DerivedAttributeA()] 
     public string PropertyB { get; set; } // not allowed, concrete classes the same, honours AllowMultiple=false on BaseAttribute 
    } 

답변

6

문제는 AllowMultiple 검사는 동일한 실제 유형 (즉 콘크리트 형 인스턴스)의 특성을 비교하는 것이 단순히 - 아마 최고의 이런 이유로 sealed 속성으로 사용됩니다.

그것은 예를 들어, 시행 할 BaseAttribute에서이 상속, (불법 중복으로) 다음 : 짧은에서

[DerivedAttributeB()] 
[DerivedAttributeB()] 
public string Name { get; set; } 

, 난 당신이 당신이 원하는 것을 할 수 있다고 생각하지 않습니다 ... (속성 당의 BaseAttribute을 포함하여 하나의 인스턴스 을 적용하지 않아도됩니다.)

이 문제의 유사한 예는 다음과 같습니다

[Description("abc")] 
[I18NDescriptionAttribute("abc")] 
public string Name { get; set; } 

class I18NDescriptionAttribute : DescriptionAttribute { 
    public I18NDescriptionAttribute(string resxKey) : base(resxKey) { } 
} 

위의 목적은 (완전 ComponentModel 등 지원) 런타임에 RESX에서 [Description]을 제공하는 것입니다 - 또한 수 없습니다 중지하면되지만 [Description]을 추가하십시오.

+0

나는 그것이 사실일지도 모른다는 것을 두려워했다, 확인을위한 감사. –

관련 문제