1

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

Community
  • 1
  • 1
Karinne
  • 249
  • 2
  • 10
  • What is Resources.Enums? – mDC Jul 14 '15 at 11:58
  • It's my resource page with the full names for this particular enum. – Karinne Jul 14 '15 at 12:09
  • 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 Jul 14 '15 at 12:34
  • I get an error with just Resources in there saying "'Resources' is a 'namespace' but is used like a 'type'" – Karinne Jul 14 '15 at 13:05
  • 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 Jul 14 '15 at 13:43
  • I have my resource pages (Enums.resx and Enums.fr-CA.resx) in the App_GlobalResources. Didn't do anything different really. They work everywhere else in the app. Thanks for all the help BTW. – Karinne Jul 14 '15 at 13:48
  • Try this: click the Enums.resx (and subsequently all other *.resx you want to access this way) and open the properties window. "Build Action" should say "Embedded Resource", I think. – mDC Jul 14 '15 at 14:00
  • Did that. Getting the same error. What about the Custom Tool property? What is that one supposed to be set as? I remember I had to change some of my other resources files. – Karinne Jul 14 '15 at 14:14
  • According to that: http://stackoverflow.com/questions/1327692/c-what-does-missingmanifestresourceexception-mean-and-how-to-fix-it – mDC Jul 14 '15 at 14:15

1 Answers1

1

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;
    }
}
Jamie Dunstan
  • 3,725
  • 2
  • 24
  • 38
  • Thank for the help. I now get this error "An exception of type 'System.MissingMethodException' occurred in mscorlib.dll but was not handled in user code ... Additional information: No parameterless constructor defined for this object' – Karinne Jul 14 '15 at 13:23
  • @Karinne - try now. I've replaced the Activator class with a call to `System.Runtime.Serialization.FormatterServices.GetUninitializedObject`. – Jamie Dunstan Jul 14 '15 at 13:55
  • Ok... now I get "An exception of type 'System.NullReferenceException' occurred in FWS.dll but was not handled in user code Additional information: Object reference not set to an instance of an object." – Karinne Jul 14 '15 at 14:14
  • If you call `Assembly.GetExecutingAssembly().GetManifestResourceNames()` in the immediate window when you hit your exception, do you see your Enums resource file in the list? Does your Enums resource file have a `Custom Tool Namespace`? – Jamie Dunstan Jul 14 '15 at 14:36
  • Yes my enums file is in the list {string[4]} [0]: "FWS.App_GlobalResources.Enums.resources" Custom Tool Namespace is empty – Karinne Jul 14 '15 at 17:38
  • @Karinne - I have made another attempt... please have a look at the new code and see how you get one. – Jamie Dunstan Jul 15 '15 at 07:23