0

I have some enums which i have set up as follows:

public enum MyDefaultEnums
{
    [EnumMember(Value="My First Enum")]enum1,
    [EnumMember(Value="My Second Enum")]enum2,
}

Then I run a method which checks a given textbox (tb) to see what text is in it. If the value is there in the enum list, it clears it, if it isn't, it leaves it alone:

if (Enum.IsDefined(typeof(MyDefaultEnums), tb.Text) == true)
        {
            tb.Text = "";  
        }

However, its not working. Debugging at runtime shows me that MyDefaultEnums is picking the values up as enum1 and enum2, instead of the strings i put in there. Can anyone point me to where im going wrong? Thanks

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Ricardinho
  • 53
  • 2
  • 10
  • 5
    This [SO post](http://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value) should show you how to do it. – Michael May 08 '15 at 10:48
  • Hmm - that post implies that it is assigning a var (meminfo) to each enum attribute individually (NameWithoutSpaces1) - or am i missing something? As my enum grows, that's not something i want to do – Ricardinho May 08 '15 at 10:57
  • and if you have to do it that way, whats the point in setting the enum value via EnumMember(Value= ... – Ricardinho May 08 '15 at 10:58

1 Answers1

0

You can get all custom attribute values

var enumValues = typeof(MyDefaultEnums)
    .GetFields()
    .SelectMany(f => f.GetCustomAttributes(typeof(EnumMemberAttribute), false))
    .Cast<EnumMemberAttribute>()
    .Select(a => a.Value);

Then just check if your value in that array

if (enumValues.Contains(tb.Text))
    tb.Text = ""; 

You can put this code into helper class

public class Enum<TEnum>
{
    public static IEnumerable<string> GetMemberValues()
    {
        return typeof(TEnum).GetFields()
            .SelectMany(f => f.GetCustomAttributes(typeof(EnumMemberAttribute), false))
            .Cast<EnumMemberAttribute>()
            .Select(a => a.Value);
    }
}

Now check is simple as

if (Enum<MyDefaultEnums>.GetMemberValues().Contains(tb.Text))
    tb.Text = "";

And you can use it with any enum marked with EnumMember attributes

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • IDE isn't recognizing .GetFields(). MSDN implies its in the system namespace, which is obviously set, but its not having it – Ricardinho May 08 '15 at 11:09
  • @Ricardinho make sure [GetFields](https://msdn.microsoft.com/en-us/library/ch9714z3%28v=vs.110%29.aspx) is available in your framework. Is it Mono? Also make sure you didn't miss `typeof` part – Sergey Berezovskiy May 08 '15 at 11:15
  • I can see it in the references under system.type, which im using in the project, but its not being recognised. Not sure what you mean by mono though – Ricardinho May 08 '15 at 11:24
  • The error im seeing is 'System.Type' does not contain a definition for 'GetFields' and no extension method 'GetFields' accepting a first argument of type 'System.Type' could be found (are you missing a using directive or an assembly reference?) – Ricardinho May 08 '15 at 11:25
  • Yeah - i have used typeof – Ricardinho May 08 '15 at 11:26
  • @Ricardinho maybe you haven't specified that you are developing Win Phone application? – Sergey Berezovskiy May 08 '15 at 11:29
  • @Ricardinho if my guess is correct, then according to [Evolving Reflection API](http://blogs.msdn.com/b/dotnet/archive/2012/08/28/evolving-the-reflection-api.aspx) you should use GetTypeInfo – Sergey Berezovskiy May 08 '15 at 11:45
  • yeah, your right, its a windows phone app (sorry for the confusion). After adding system.reflection, i do get GetTypeInfo(). But then it doesnt recognise selectionmany ... – Ricardinho May 08 '15 at 12:21
  • @Ricardinho its a `System.Linq` namespace – Sergey Berezovskiy May 08 '15 at 12:24
  • So should i still be able to run selectmany with GetTypeInfo()? Ive added using system.Linq to my project, but its still telling my that system.reflection.typeinfo does not contain a definition for SelectMany – Ricardinho May 08 '15 at 12:47
  • @Ricardinho `SelectMany` is an extension method. See `Linq` – Sergey Berezovskiy May 08 '15 at 12:53