-1

I am making c# library using dotNet5. Problem is i want to declare enum in my library that i want share across my projects via nuget package.

 public enum Roles
        {
            [Value("Root")]
            Root,
            [Value("Team Member")]
            TeamMember,
            [Value("Team Lead")]
            TeamLead

         }

When i import the nuget package in another project i get following

public enum Roles
        {
            Root = 0,
            TeamMember = 1,
            TeamLead = 2,
        }

Why attribute is missing when importing the nuget package ? Also is there another way to solve this issue. i am creating attribute in following way

[System.AttributeUsage(System.AttributeTargets.Field)]
    public class ValueAttribute : System.Attribute
    {
        public ValueAttribute(string name)
        {
            this.Name = name;
        }

        public string Name { get; internal set; }
    }
Azerue
  • 258
  • 6
  • 16
  • It seems like the two `enum`s are different. If they were the same they'd have the same values. – Enigmativity Jul 29 '21 at 11:42
  • 3
    "When i import the nuget package in another project i get following" - what *exactly* do you mean by that? How are you observing that? Have you tried using reflection to access the attributes? A [mcve] would be really helpful here. – Jon Skeet Jul 29 '21 at 11:46
  • `ValueAttribute` is a custom attribute that you add to the enum values. It doesn't actually change the enum value. When you don't provide explicit values enum values start from 0 and increment by 1. So basically those two enums are identical. – Eldar Jul 29 '21 at 11:47
  • 1
    @Eldar: I think the point is that the OP wants to access the attribute from within the other codebase. But we don't know how they're trying to do that or why they believe it's missing at the moment... – Jon Skeet Jul 29 '21 at 11:51
  • Maybe try accessing the attributes using reflection: [example](https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property) – Michael Rath Jul 29 '21 at 12:01
  • When i inspect the nuget package from other code base. There i only see the enum with int values and not the attribute which should be there. – Azerue Jul 29 '21 at 12:02
  • I haven't tried reflection yet but why attribute is missing when enum with custom attribute is imported via nuget. – Azerue Jul 29 '21 at 12:04
  • @Azerue `When i inspect the nuget package from other code base.` what does that mean? Did you use a decompiler? VS's Object Browser? Unless the package deploys source code, you can't inspect an assembly without decompiling it or using Reflection. Please post code that demonstrates the problem – Panagiotis Kanavos Jul 29 '21 at 12:11
  • @Azerue in any case, if you want to specify a display name you. can use the built-in [Display](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayattribute?view=net-5.0) attribute. Web applications for example will use it to display the enum values – Panagiotis Kanavos Jul 29 '21 at 12:12
  • @Panagiotis Kanavos I am using VSCode on mac. So i just cmd + click on Enum to inspect and it was showing the above share enum with no attributes. – Azerue Jul 29 '21 at 12:14
  • @Azerue that doesn't mean they aren't there. It means VS Code's Object Explorer doesn't show them. Have you tried to actually use them? Why create your own attribute instead of using `Display` though? Formatters and renderers understand it already – Panagiotis Kanavos Jul 29 '21 at 12:15
  • For example `Html.GetEnumSelectList()` will return the display names ready for use in a dropdown – Panagiotis Kanavos Jul 29 '21 at 12:21

1 Answers1

0

Value is indeed returned when using

public static string GetValue<T>(this T enumerationValue)
            where T : System.Enum
        {
            Type type = enumerationValue.GetType();
            if (!type.IsEnum)
            {
                throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
            }

            //Tries to find a DescriptionAttribute for a potential friendly name
            //for the enum
            MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
            if (memberInfo != null && memberInfo.Length > 0)
            {
                object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);

                if (attrs != null && attrs.Length > 0)
                {
                    //Pull out the description value
                    return ((DisplayAttribute)attrs[0]).Name;
                }
            }
            //If we have no description attribute, just return the ToString of the enum
            return enumerationValue.ToString();
        }

I misunderstood when i cmd + clicked. Also now using Display as recommended by @Panagiotis Kanavos. thank you

Azerue
  • 258
  • 6
  • 16