1
votes

Giving this one more try

I have an Enums.cs file where I have the following code

public enum AuthorizingLevels
    {
        [Display(Name = "AuthorizingLevels_SysSupport", ResourceType = typeof(Resources.Enums))]
        SysSupport
    }

And when I try calling it to display the Name, it doesn't work

ViewBag.AuthGroupFullName = Enums.AuthorizingLevels.SysSupport.ToString();

It just displays the SysSupport instead of the full name Systems Support

I went to a link provided in my previous question (How to get the Display Name Attribute of an Enum member via MVC razor code?) and added the code by Peter Kerr

/// <summary>
    ///     A generic extension method that aids in reflecting 
    ///     and retrieving any attribute that is applied to an `Enum`.
    /// </summary>
    public static string GetDisplayName(this Enum enumValue)
    {
        var displayAttrib = enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<DisplayAttribute>();
        var name = displayAttrib.Name;
        var resource = displayAttrib.ResourceType;

        return String.IsNullOrEmpty(name) ? enumValue.ToString()
            : resource == null ? name
            : new ResourceManager(resource).GetString(name);
    }

However, I get an error on the line

: new ResourceManager(resource).GetString(name);

An exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll but was not handled in user code Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resources.Enums.resources" was correctly embedded or linked into assembly "FWS" at compile time, or that all the satellite assemblies required are loadable and fully signed.

The resource file is the right one minus the .resources at the end... not sure if that should be there or not.

What am I doing wrong? I'm learning MVC and c# as I go so any help would be greatly appreciated.

Thank you

1
What is Resources.Enums?mDC
It's my resource page with the full names for this particular enum.Karinne
I think this is your problem. I just pasted your code in a fresh c# console project, put the display-name in the standard resource and said [Display(Name = "AuthorizingLevels_SysSupport", ResourceType = typeof(Resources))]. That works fine.mDC
I get an error with just Resources in there saying "'Resources' is a 'namespace' but is used like a 'type'"Karinne
Hm, in an out-of-the-box project, "Resources" is a standard class that is defined by Visual Studio (normally in Resources.Designer.cs). So what did you do to have resouce pages?mDC

1 Answers

1
votes

Try this. It is fairly hacky but it should hopefully give you what you need. I use Assembly.GetExecutingAssembly().GetManifestResourceNames() to get the names of all the resources in the executing assembly and then I attempt to match the file up correctly using some linq. If it finds a file that looks ok, a new ResourceManager is created as you were using in your original code.

/// <summary>
///     A generic extension method that aids in reflecting 
///     and retrieving any attribute that is applied to an `Enum`.
/// </summary>
public static string GetDisplayName(this Enum enumValue)
{
    var displayAttrib = enumValue.GetType()
        .GetMember(enumValue.ToString())
        .First()
        .GetCustomAttribute<DisplayAttribute>();

    var name = displayAttrib.Name;
    if (String.IsNullOrEmpty(name))
    {
        return enumValue.ToString();
    }
    else
    {
        var resource = displayAttrib.ResourceType;
        if (resource != null)
        {
            var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames()
                .Where(x => x.EndsWith(String.Format("{0}.resources", resource.Name)))
                .Select(x => x.Replace(".resources", string.Empty)).ToList();
            if (resources.Any())
            {
                return new ResourceManager(resources.First(), Assembly.GetExecutingAssembly()).GetString(name);
            }
        }

        return name;
    }
}