Search code examples
c#attributesambiguous

c# ambiguous attributes


i read the following great question: Attributes in C# and learned a lot about attributes.

i am using a library that uses several attributes. example:

[State]
public class USStates
{
    [City]
    public void isNewYork() { }
    [Zip]
    public ZipCode GetZipCode() { }
}

i wanted to create my own attributes and perform additional work on my classes using existing attributes (State, City, Zip) which are from a 3rd party namespace. for example, i'm trying to achieve this:

using 3rdPartyNamespace;
using MyOwnSuperNamespace;

public class State : Attribute 
{ 
    // my own implementation/extension of State 
}

// create City, Zip attributes respectively

[State]
public class USStates
{
    [City]
    public void isNewYork() { }
    [Zip]
    public ZipCode GetZipCode() { }
}

i tried the above but keeping getting an ambiguous attribute error on all 3 attributes. ho can i tell the compiler to direct call to both attributes instead of using either one? i would like to keep the existing attributes and perform additional work without having to mark my classes with additional attributes that i have created.

is this possible?


Solution

  • ho can i tell the compiler to direct call to both attributes instead of using either one?

    If you want to apply both attributes, you need to specify both attributes. You'll need to either fully-qualify the attribute names, or use using alias directives, e.g.

    using Mine = MyOwnSuperNamespace;
    using Theirs = 3rdPartyNamespace;
    
    [Mine.State]
    [Theirs.State]
    ...