I have an enum annotated with EnumMember to facilitate JSON.NET serialization similar to the following:
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum Status
{
[EnumMember(Value = "NOT_ADMITTED")]
NotAdmitted,
[EnumMember(Value = "ADMITTED")]
Admitted
}
Now, independent of the JSON.NET serialization I'd like to I'd like to convert instances of the enum to a string while abiding by the EnumMember annotations in the data contract, e.g.:
aStatusInstance.ToString() == "NOT_ADMITTED".
Any suggestions? Thanks!
Update: My Solution
I modified the code in the accepted answer to create an extension method to retrieve the EnumMember Value:
public static string GetEnumMemberValue(this Enum enumValue)
{
var type = enumValue.GetType();
var info = type.GetField(enumValue.ToString());
var da = (EnumMemberAttribute[])(info.GetCustomAttributes(typeof(EnumMemberAttribute), false));
if (da.Length > 0)
return da[0].Value;
else
return string.Empty;
}
I would use the Description Attribute and decorate the enum with the same value as the EnumMember:
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum Status
{
[EnumMember(Value = "NOT_ADMITTED")]
[Description("NOT_ADMITTED")]
NotAdmitted,
[EnumMember(Value = "ADMITTED")]
[Description("ADMITTED")]
Admitted
}
You can use this code snippet to parse it. This is written as an extension of the Enum class:
public static string GetDescription(this Enum enumValue)
{
Type type = enumValue.GetType();
FieldInfo info = type.GetField(enumValue.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])(info.GetCustomAttributes(typeof(DescriptionAttribute), false));
if (da.Length > 0)
return da[0].Description;
else
return string.Empty;
}
You can then compare it with the following:
aStatusInstance.GetDescription() == "NOT_ADMITTED"
Related
This is my code:
[DataContract] // (Name = "Type")]
public enum Purpose
{
[EnumMember(Value = "definitionTarget")]
DefinitionTarget = 0,
[EnumMember(Value = "definitionSource")]
DefinitionSource = 1,
[EnumMember(Value = "semanticRole")]
SemanticRole = 2,
[EnumMember(Value = "dataType")]
DataType = 3
}
I want the enum values to display according to the strings given, not the integer values. For some reason, the values are ignored.
The serialization code is nothing fancy:
protected string GetRuntimeValue(RuntimeValue value)
{
MemoryStream ms = new MemoryStream();
_serializer.WriteObject(ms, value);
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
I went to the Microsoft documentation and found an example with some boilerplate code where they inherit IExtensibleDataObject (no explanation why). I added the code to my base class, no change.
What am I doing wrong? Should be something simple, no?
The bad news is that DataContractJsonSerializer ignores the EnumMember attribute and this is by design:
Enumeration member values are treated as numbers in JSON, which is
different from how they are treated in data contracts, where they are
included as member names. ... The EnumMemberAttribute and the NonSerializedAttribute attributes are ignored if used.
A Microsoft support agent also confirmed this and suggested a kludge which is to expose a property decorated with DataMember for the serializer to use instead, which calls Enum.GetName() to get the string name of the enum value.
It would need to be modified in your case to return a string with the first letter converted to lower case:
[DataContract]
public class RuntimeValue
{
public Purpose Purpose { get; set; }
[DataMember(Name = "Purpose")]
string PurposeString
{
get { return Enum.GetName(typeof(Purpose), this.Purpose).FirstCharacterToLower(); }
set { this.Purpose = (Purpose)Enum.Parse(typeof(Purpose), value, true); }
}
}
Uses an extension method borrowed from here to do the lower case stuff:
public static string FirstCharacterToLower(this string str)
{
if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
return str;
return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}
Testing:
Console.WriteLine(GetRuntimeValue(new RuntimeValue()));
Output:
{"Purpose":"definitionTarget"}
Personally I would use JSON.NET instead if possible but for the sake of my answer I am assuming that you have already considered that option and have good reason to stick with this serializer.
I'm using ServiceStack v 3.9.71 and the ServiceStack.Text.EnumMemberSerializer assembly to serialize enums into readable text.
This works great, my enum values are serialized into the name I've specified using the EnumMemberAttribute.
The problem, though, is Swagger does not use my names. My guess is it just calls the .ToString() method on the enum values rather than the EnumMemberAttribute value.
Here is the order in which I setup the serialization. (In AppHost):
new EnumSerializerConfigurator()
.WithEnumTypes(new Type[] { typeof(MyEnum) })
.Configure();
Plugins.Add(new SwaggerFeature());
It doesn't seem to matter if the enum serializer is set before or after the swagger feature is added.
You are correct that the Swagger code does not use ServiceStack.Text.EnumMemberSerializer when parsing enum values. It only uses an Enum.GetValues here. Note that this is still the same in v4.
You can submit a pull request to make this change, but I'm not familiar with EnumMemberSerialzer and how it allows for retrieving the list of enum options. You may instead be able to use a string property decorated with ApiAllowableValues to achieve the affect.
Here is the solution I came up with (with the help of bpruitt-goddard, thanks mate):
The enum:
public enum MyEnum
{
[EnumMember(Value = "Value One")]
Value1 = 1,
[EnumMember(Value = "Value Two")]
Value2 = 2,
[EnumMember(Value = "Value Three")]
Value3 = 3
}
The client object:
public class MyClientObject
{
[Description("The name")]
public string Name {get;set;}
[Description("The client object type")]
[ApiAllowableValues("MyEnum", "Value One", "Value Two", "Value Three")]
public MyEnum MyEnum { get; set; }
}
Inside the AppHost:
new EnumSerializerConfigurator()
.WithEnumTypes(new Type[] { typeof(MyEnum) })
.Configure();
Now the enum is serialized properly and the Swagger documentation is correct. The only issue with this is having the names in two different places. Perhaps there is a way to check the names match via a unit test.
I came up with, in my opinion, a better solution. I wrote a class that extends the ApiAllowableValuesAttribute:
public class ApiAllowableValues2Attribute : ApiAllowableValuesAttribute
{
public ApiAllowableValues2Attribute(string name, Type enumType)
: base(name)
{
List<string> values = new List<string>();
var enumTypeValues = Enum.GetValues(enumType);
// loop through each enum value
foreach (var etValue in enumTypeValues)
{
// get the member in order to get the enumMemberAttribute
var member = enumType.GetMember(
Enum.GetName(enumType, etValue)).First();
// get the enumMember attribute
var enumMemberAttr = member.GetCustomAttributes(
typeof(System.Runtime.Serialization.EnumMemberAttribute), true).First();
// get the enumMember attribute value
var enumMemberValue = ((System.Runtime.Serialization.EnumMemberAttribute)enumMemberAttr).Value;
values.Add(enumMemberValue);
}
Values = values.ToArray();
}
}
The client object:
public class MyClientObject
{
[Description("The name")]
public string Name {get;set;}
[Description("The client object type")]
[ApiAllowableValues2("MyEnum", typeof(MyEnum))]
public MyEnum MyEnum { get; set; }
}
Now you don't have to specify the names again or worry about a name change breaking your Swagger documentation.
I'm trying to add new/extension method for Enum but the extension method is not showing on intellisense method list. Please help here's my code.
Extension:
public static class EnumExtensions
{
public static string GetDescriptionAttr(this Enum value,string key)
{
var type = value.GetType();
var memInfo = type.GetMember(key);
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),
false);
var description = ((DescriptionAttribute)attributes[0]).Description;
return description;
}
}
Trying to call the result from other class (both caller and extension are in the same project)
Extension methods can be applied on instances only
public static class EnumExtensions {
// This extension method requires "value" argument
// that should be an instance of Enum class
public static string GetDescriptionAttr(this Enum value, string key) {
...
}
}
...
public enum MyEnum {
One,
Two,
Three
}
Enum myEnum = MyEnum.One;
// You can call extension method on instance (myEnum) only
myEnum.GetDescriptionAttr("One");
You should use extension method for an instance of your enum.
I have this code and it works properly:
public static string GetDescription(this Enum value)
{
var attributes =
(DescriptionAttribute[])value.GetType().GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
And using of this method shows here:
MyEnum myE = MyEnum.OneOfItemsOfEnum;
string description = myE.GetDescription();
I have this variant
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
namespace Web.Extensions
{
public static class EnumExtension
{
public static string GetDisplayName(this Enum enumValue)
{
var displayName = enumValue.GetType()?
.GetMember(enumValue.ToString())
.FirstOrDefault()?
.GetCustomAttribute<DisplayAttribute>()?
.Name;
return displayName ?? enumValue.ToString();
}
}
}
enum definition
public enum SomeEnum
{
One= 1,
[Display(Name = "Second")]
Two= 2,
Three= 3
}
and in view (extension called directly).
display name from enum
#Web.Extensions.EnumExtension.GetDisplayName(Domain.Enums.SomeEnum.Two)
show display name from enum id
#Web.Extensions.EnumExtension.GetDisplayName((Domain.Enums.SomeEnum)2)
show display name from enum id without display attribute
#Web.Extensions.EnumExtension.GetDisplayName((Domain.Enums.SomeEnum)3)
outputs
Second
Second
Three
or more simply
#using Web.Extensions
#using Domain.Enums
#SomeEnum.Two.GetDisplayName()
#(((SomeEnum)2).GetDisplayName())
I know following syntax is possible with enum, and one can get value by parsing it in int or char.
public enum Animal { Tiger=1, Lion=2 }
public enum Animal { Tiger='T', Lion='L' }
Although following syntax is also right
public enum Anumal { Tiger="TIG", Lion="LIO"}
How do I get the value in this case? If I convert it using ToString(), I get the KEY not the VALUE.
If you really insist on using enum to do this, you can do it by having a Description attribute and getting them via Reflection.
public enum Animal
{
[Description("TIG")]
Tiger,
[Description("LIO")]
Lion
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
Then get the value by string description = GetEnumDescription(Animal.Tiger);
Or by using extension methods:
public static class EnumExtensions
{
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
Then use it by string description = Animal.Lion.GetEnumDescription();
You can't use strings in enums. Use one or multiple dictionaries istead:
Dictionary<Animal, String> Deers = new Dictionary<Animal, String>
{
{ Animal.Tiger, "TIG" },
{ ... }
};
Now you can get the string by using:
Console.WriteLine(Deers[Animal.Tiger]);
If your deer numbers are in line ( No gaps and starting at zero: 0, 1, 2, 3, ....) you could also use a array:
String[] Deers = new String[] { "TIG", "LIO" };
And use it this way:
Console.WriteLine(Deers[(int)Animal.Tiger]);
Extension method
If you prefer not writing every time the code above every single time you could also use extension methods:
public static String AsString(this Animal value) => Deers.TryGetValue(value, out Animal result) ? result : null;
or if you use a simple array
public static String AsString(this Animal value)
{
Int32 index = (Int32)value;
return (index > -1 && index < Deers.Length) ? Deers[index] : null;
}
and use it this way:
Animal myAnimal = Animal.Tiger;
Console.WriteLine(myAnimal.AsString());
Other possibilities
Its also possible to do the hole stuff by using reflection, but this depends how your performance should be ( see aiapatag's answer ).
That is not possible, the value of the enum must be mapped to a numeric data type. (char is actually a number wich is wirtten as a letter)
However one solution could be to have aliases with same value such as:
public enum Anumal { Tiger=1, TIG = 1, Lion= 2, LIO=2}
Hope this helps!
This isn't possible with Enums. http://msdn.microsoft.com/de-de/library/sbbt4032(v=vs.80).aspx
You can only parse INT Values back.
I would recommend static members:
public class Animal
{
public static string Tiger="TIG";
public static string Lion="LIO";
}
I think it's easier to handle.
As DonBoitnott said in comment, that should produce compile error. I just tried and it does produce. Enum is int type actually, and since char type is subset of int you can assign 'T' to enum but you cannot assign string to enum.
If you want to print 'T' of some number instead of Tiger, you just need to cast enum to that type.
((char)Animal.Tiger).ToString()
or
((int)Animal.Tiger).ToString()
Possible alternative solution:
public enum SexCode : byte { Male = 77, Female = 70 } // ascii values
after that, you can apply this trategy in your class
class contact {
public SexCode sex {get; set;} // selected from enum
public string sexST { get {((char)sex).ToString();}} // used in code
}
My enum structure is this
public enum UserRole
{
Administrator = "Administrator",
Simple_User = "Simple User",
Paid_User = "Paid User"
}
Now i want to read this enum value by using its name suppose
String str = UserRole.Simple_User;
it gives me "Simple User" in str instead of "Simple_User"
How we can do this???
You can do a friendly description like so:
public enum UserRole
{
[Description("Total administrator!!1!one")]
Administrator = 1,
[Description("This is a simple user")]
Simple_User = 2,
[Description("This is a paid user")]
Paid_User = 3,
}
And make a helper function:
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] info = type.GetMember(en.ToString());
if (info != null && info.Length > 0)
{
object[] attrs = info[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
And use it like:
string description = GetDescription(UserRole.Administrator);
Okay so by now you know that enum really is a list of numbers that you can give a handy string handle to like:
public enum ErrorCode
{
CTCLSM = 1,
CTSTRPH = 2,
FBR = 3,
SNF = 4
}
Also, as #StriplingWarrior showed, you can go so far by getting the enum string name and replacing underscores etc. But what I think you want is a way of associating a nice human string with each value. How about this?
public enum ErrorCode
{
[EnumDisplayName("Cataclysm")]
CTCLSM = 1,
[EnumDisplayName("Catastrophe")]
CTSTRPH = 2,
[EnumDisplayName("Fubar")]
FBR = 3,
[EnumDisplayName("Snafu")]
SNF = 4
}
Okay there's probably something in System.ComponentModel that does this - let me know. The code for my solution is here:
[AttributeUsage(AttributeTargets.Field)]
public class EnumDisplayNameAttribute : System.Attribute
{
public string DisplayName { get; set; }
public EnumDisplayNameAttribute(string displayName)
{
DisplayName = displayName;
}
}
And the funky Enum extension that makes it possible:
public static string PrettyFormat(this Enum enumValue)
{
string text = enumValue.ToString();
EnumDisplayNameAttribute displayName = (EnumDisplayNameAttribute)enumValue.GetType().GetField(text).GetCustomAttributes(typeof(EnumDisplayNameAttribute), false).SingleOrDefault();
if (displayName != null)
text = displayName.DisplayName;
else
text = text.PrettySpace().Capitalize(true);
return text;
}
So to get the human-friendly value out you could just do ErrorCode.CTSTRPH.PrettyFormat()
Hmm, enums can't have string values. From MSDN's Enum page:
An enumeration is a set of named constants whose underlying type is any integral type except Char.
To get the string version of the enum use the Enum's ToString method.
String str = UserRole.Simple_User.ToString("F");
I'm a little confused by your question, because C# doesn't allow you to declare enums backed by strings. Do you mean that you want to get "Simple User", but you're getting "Simple_User" instead?
How about:
var str = UserRole.Simple_User.ToString().Replace("_", " ");
You can do this by attribute, but really I think using an enum like this is possibly the wrong way to go about things.
I would create a user role interface that exposes a display name property, then implement that interface with a new class for each role, this also let's you add more behaviour in the future.
Or you could use an abstract class so any generic behaviour doesn't get duplicated...