-1

Edit: I updated the post to use a Guid instead of int. Thanks to those below who have pointed out that using int doesn't do anything since the base type of the enum is int.

I have an enum, let's say it's

public enum AnimalType
{
    [Animal("00000000000000000000000000000001")]
    Bear = 0,

    [Animal("00000000000000000000000000000002")]
    Cat = 1,

    [Animal("00000000000000000000000000000003")]
    Dog = 2
}

This enum uses the Animal attribute, defined as:

public class AnimalAttribute : Attribute
{
    public AnimalAttribute(Guid id)
    {
        this.AnimalId = id;
    }

    public AnimalAttribute(string id)
    {
        this.AnimalId = new Guid(id);
    }

    public Guid AnimalId { get; private set; }

    // Some more properties

}

As you can see there are two constructors for AnimalAttribute, allowing to accept the id as a string or Guid. The AnimalType enum uses the string constructor.

So as of now the enum value can only be accessed by knowing the id. Let's say that if I don't know the id corresponding to a particular enum value, I can also access the enum values using some name/description corresponding to each enum value (probably just "Bear" for Bear, etc.). My question is, how do I set this up so the AnimalAttribute can accept either the id (and either as a string or a Guid) or the name/description corresponding to the enum value? I would like to make another constructor for AnimalAttribute like so:

public AnimalAttribute(string name)
{
    this.AnimalName = name; // assume there is an AnimalName property.
}

Then I could add another attribute to each enum value like so:

[Animal("00000000000000000000000000000001")]
[Animal("Bear")]
Bear = 0,

And so I could supply either value and I'll be able to get the enum value. The problem with this is that I can't make the constructor in AnimalAttribute accepting string name since there is already another constructor accepting string id. If I can't do that then I thought I could make the constructor with optional parameters. Is there an easy way to set this up?

Drew
  • 1,277
  • 3
  • 21
  • 39

2 Answers2

1

Not sure what you're goal is, but you may be over complicating this. It doesn't make sense to use an enumeration with its int value then define an attribute with a different int value.

Look at using a DescriptionAttribute instead. It's built-in and will do what you need.

Check out Getting attributes of Enum's value

Community
  • 1
  • 1
Stinky Towel
  • 768
  • 6
  • 26
  • Oops, I may have dumbed down what I'm actually working with to the extent that this example is actually slightly different. The int id is actually a Guid, which is why it's necessary. So basically my question is, I want to get the value of an enum with either a Guid or a name/description attribute -- is that possible? Does that make sense? – Drew Oct 11 '16 at 15:42
  • **DescriptionAttribute** will still work. You're guid is just a string, so that can be the description. I question your end goal though. Why not have an Interface or Abstract type of Animal instead and keep the Guid there? – Stinky Towel Oct 11 '16 at 16:15
  • I guess I'm still a bit confused. Maybe a bit more background of what I'm actually working with will help. I am parsing an XmlDocument and in this document I have access to the animal name (which doesn't directly correspond to the enum names, so I can't use `Enum.TryParse`). I do not have access to a Guid corresponding to the animal. So even if the Guid could be the description, that doesn't help me with getting the enum value. As to your second point, I'm just working with code that has been given to me. I don't want to change it because other stuff relies on it, but I can add to it. – Drew Oct 11 '16 at 19:28
0

Forgive me if I'm misunderstanding some details of your scenario, but if the AnimalId property of the Animal attribute is always going to equal the int value of the Enum element then isn't the Animal attribute unnecessary? Couldn't you just do the following:

Enum:

public enum AnimalType
{
    Bear = 0,
    Cat = 1,
    Dog = 2
}

To get the int value just cast the enum:

int animalVal = (int)AnimalType.Bear;

To get the animal name by value:

string animalName = Enum.GetName(typeof(AnimalType), 0);
Dave
  • 70
  • 8
  • Yes, you're right, that's my mistake. I made up this example because I don't want to give the actual thing I'm working with. The id I'm actually working with is a Guid, which is why it's necessary. See my comment on the answer above for more clarification. – Drew Oct 11 '16 at 15:44
  • @Drew I have referred to your comments above for clarification. Since you don't have access to the Guid corresponding to the animal, why don't you just decorate each enum with a description attribute that contains the animal name? Then you can get the enums that way. – Dave Oct 11 '16 at 20:54