asp.net mvc - Displaying the full name with Enums and resource file in c# MVC -
giving 1 more try
i have enums.cs file have following code
public enum authorizinglevels { [display(name = "authorizinglevels_syssupport", resourcetype = typeof(resources.enums))] syssupport }
and when try calling display name, doesn't work
viewbag.authgroupfullname = enums.authorizinglevels.syssupport.tostring();
it displays syssupport instead of full name systems support
i went link provided in previous question (how display name attribute of enum member via mvc razor code?) , added code peter kerr
/// <summary> /// generic extension method aids in reflecting /// , retrieving attribute applied `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, error on line
: new resourcemanager(resource).getstring(name);
an exception of type 'system.resources.missingmanifestresourceexception' occurred in mscorlib.dll not handled in user code additional information: not find resources appropriate specified culture or neutral culture. make sure "resources.enums.resources" correctly embedded or linked assembly "fws" @ compile time, or satellite assemblies required loadable , signed.
the resource file right 1 minus .resources @ end... not sure if should there or not.
what doing wrong? i'm learning mvc , c# go appreciated.
thank you
try this. hacky should give need. use assembly.getexecutingassembly().getmanifestresourcenames()
names of resources in executing assembly , attempt match file correctly using linq. if finds file looks ok, new resourcemanager
created using in original code.
/// <summary> /// generic extension method aids in reflecting /// , retrieving attribute applied `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; } }
Comments
Post a Comment