Reflection in C# - c#

I have recently started a development in c# and want to use reflection in following situation.
If I have a Enum class as
Enum Operation
{
Read=0;
Write;
}
If I give input as
String str = "Operation.Write";
I shoud be able to get output as 1;
Or
if constants are defined like
const int Read=0;
const int Write=1;
If the input is
String str = "Read";
output should be 0
Please Help.

You can use Enum.Parse to have that functionality.
If we combine your proposals we can get something like this.
public static Operation getOperationByName(String name) {
return Enum.Parse(typeof(Operation),name);
}
Where the name should not be null and represent the name or position in enum ie
"Read" will return Operation.Rerad and "1" will return Operation.Write

Heres the complete code to also Get the type of the Enum through Reflection without hardcoding it. The ParseConstant Method is also generic, s.t. you can use if for every Type.
namespace MyNamgespace
{
public enum Operation
{
Read = 0,
Write
}
public class ClassWithConstants
{
public const int Read = 0;
public const int Write = 1;
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine((ParseEnum("Operation.Write")));
Console.WriteLine((ParseContant<ClassWithConstants>("Write")));
Console.ReadLine();
}
static int ParseEnum(string enumValue)
{
var typeName = enumValue.Split('.')[0];
var valueName = enumValue.Split('.')[1];
var enumType = Type.GetType(string.Format("MyNamespace.{0}", typeName));
var op = (Operation) Enum.Parse(enumType, valueName);
return (int)op;
}
static int ParseContant<T>(string constantName)
{
var type = typeof (T);
var field = type.GetField(constantName, BindingFlags.Static | BindingFlags.Public);
return (int)field.GetValue(null);
}
}
}

var name = Enum.GetName(typeof(Operation), Operation.Write) //name = 'Write'
var value = Enum.Parse(typeof(Operation), "Write") //value = Operation.Write

Related

How do I access List<int> value during reflection of a class?

I am fairly new to Reflection, but have been able to retrieve all fields of my passed class. Now I am trying to retrieve the values of each field, but I'm having an issue with List<T>.
I have a simple class for testing:
public class MyTestClass
{
public string Name;
public int Age;
public bool Alive;
public List<int> Counters;
public List<string> People;
public List<Tool> Tools;
public string[] Stuff;
public Tool[] NeededTools;
public MyTestClass(string name, int age, bool alive = true)
{
Name = name;
Age = age;
Alive = alive;
Counters = new List<int>();
Counters.Add(7);
People = new List<string>();
People.Add("Seven");
Tools = new List<Tool>();
Stuff = new string[2];
NeededTools = new Tool[3];
}
}
Here is the code I am using:
private void AttachControl(object source, FieldInfo fi, Control control)
{
switch (fi.FieldType.Name)
{
case "Boolean":
(control.Controls[fi.Name] as ComboBox).SelectedIndex = (fi.GetValue(source).ToString().ToUpper() == "TRUE") ? 1 : 0;
break;
case "List`1":
Control listControl = control.Controls[fi.Name];
var listType = fi.FieldType.GetGenericArguments();
var listFields = listType[0].GetFields(
BindingFlags.Public |
BindingFlags.Instance
);
if (listFields.Length > 0)
{
AttachToControls(listFields, listControl.Controls.Cast<Control>().ToArray());
}
else
{
// *** Here is the issue ***
var values = fi.GetValue(source);
listControl.Controls[fi.Name].Text = values[0].ToString();
}
break;
default:
control.Controls[fi.Name].Text = fi.GetValue(source).ToString();
break;
}
}
When I get to Counters I can retrieve the value var values = fi.GetValue(source); and during debug I can see the List with the value 7 in it, but it states
cannot apply indexing with [] to an expression of type object on the line:
listControl.Controls[fi.Name].Text = values[0].ToString();
I assume I need to cast it, but it will not always be an int type. Do I need to write a section for every type or is there an easier way to accomplish what I need?
FYI - I am writing a Class Library that will allow me to pass any class in and auto create a form to edit all fields.
I'd suggest something along the lines of:
var bob = values as IEnumerable;
listControl.Controls[fi.Name].Text = bob?.Cast<object>()?.FirstOrDefault()?.ToString();
Since the thing you want is a string (not a specific type) then the above code will work fine (assuming values is some form of an enumerable, like a list or an array).
Note, in particular, that IEnumerable interface is this one, not the more commonly used IEnumerable<T>. This allows you to use it without a specific type.

How to get enum Type by specifying its name in String

Suppose i have this Enum:
namespace BusinessRule
{
public enum SalaryCriteria : int
{
[EnumDisplayName(DisplayName = "Per Month")]
Per_Month = 1,
[EnumDisplayName(DisplayName = "Per Year")]
Per_Year = 2,
[EnumDisplayName(DisplayName = "Per Week")]
Per_Week = 3
}
}
I have its name in a string variable like :
string EnumAtt = "SalaryCriteria";
i am trying to check if this Enum is defined by this name, and if defined i want to get its instance.i have tried like this, but type is returning null:
string EnumAtt = "SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);
i have also tried this:
string EnumAtt = "BusinessRule.SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);
any idea how i can achieve this.
To search all loaded assemblies in the current AppDomain for a given enum -- without having the fully qualified assembly name -- you can do:
public static Type GetEnumType(string enumName)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var type = assembly.GetType(enumName);
if (type == null)
continue;
if (type.IsEnum)
return type;
}
return null;
}
For instance (picking a semi-random enum which is not in my assembly):
var type1 = Type.GetType("System.Xml.Linq.LoadOptions") // Returns null.
var type2 = GetEnumType("System.Xml.Linq.LoadOptions") // Returns successfully.
You name should still include the namespace.
A LINQ-inspired answer:
public static Type GetEnumType(string name)
{
return
(from assembly in AppDomain.CurrentDomain.GetAssemblies()
let type = assembly.GetType(name)
where type != null
&& type.IsEnum
select type).FirstOrDefault();
}
The reason is that you need to go through all loaded assemblies, not only the current assembly.
This works great for me.
Type myType1 = Type.GetType("BusinessRule.SalaryCriteria");
I tried it without "EnumDisplayName" attribute.
This works well:
using System;
namespace BusinessRule
{
public enum SalaryCriteria : int
{
Per_Month = 1,
Per_Year = 2,
Per_Week = 3
}
}
namespace ConsoleApplication16
{
internal class Program
{
private static void Main()
{
string EnumAtt = "BusinessRule.SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);
Console.WriteLine(myType1.AssemblyQualifiedName);
Console.ReadLine();
}
}
}

using lambda to extract member info via System.Reflection

I am trying to use system Reflection to enable extraction of a class member (type int)
var CurrTblID = typeof(currProjData).GetMembers(
BindingFlags.Public |BindingFlags.Static
).Select(t => t.Name)...what expresssion should I put here?
this will expose an enumerator, with the interface iEnumerable and assign it to CurrTblID
so i could use foreach loop , to find which "table ID" Tid is the current one
How Can i get to a specific field name and find out its value without the foreach?
the Source for the question is in thecurrProjData class , shown below.
some background if needed for the answer :
in this code i am using a struct that enables setting Sql server Database table
with details for later use.
i couldnt find answer for that in sql server so i've made
a struct to hold a custom index - as a numeric ID for each table i will use in my application .
public class DBMetaDetails
{
public struct DbTable
{
public DbTable(string tableName, int tableId): this()
{
this.TableName = tableName;
this.TableID = tableId;
}
public string TableName { get; set; }
public int TableID { get; set; }
}
}
TableID is a custom numeric "id" i am using via that struct,
so this is how i could reference the SQL table by a custom "index" rather its database name .
public static class currProjData
{
static DBMetaDetails.DbTable CustomersMeta = new DBMetaDetails.DbTable();
static DBMetaDetails.DbTable TimesMeta = new DBMetaDetails.DbTable();
public static void SetTablesMetaDetails()
{
TimesMeta.TableID = HTtIDs.TblTimes;
TimesMeta.TableName = HTDB_Tables.TblTimes;
CustomersMeta.TableID = HTtIDs.TblCustomers;
CustomersMeta.TableName = HTDB_Tables.TblTimeCPAReport;
}
public static readonly int CustomersTid = CustomersMeta.TableID;
public static readonly string CustomersTblName = CustomersMeta.TableName;
public static readonly int TimesTid = TimesMeta.TableID;
public static readonly string TimesTblName = TimesMeta.TableName;
}
How Can i get to a specific field name and find out its value without the foreach?
Create a generic extension Method which will handle any object and then internally do a FirstOrDefault:
public static int ExtractIntProperty<T>( this T targetItem, string propertyName )
{
int result = 0;
var prop = targetItem.GetType()
.GetProperties()
.FirstOrDefault( prp => prp.Name == propertyName );
if (prop != null)
{
var val = prop.GetValue( targetItem, null );
if (val != null)
result = (int) val;
}
return result;
}
Then get the value as such (casting to show that it handles unknown objects)
object detail = (object) new DBMetaDetails.DbTable() { TableName = "Jabberwocky", TableID = 12 };
Console.WriteLine ( detail.ExtractIntProperty("TableID") ); // 12

C# Getting the Type of a Public Variable based on an Enum value

I have a class that parses in data from a comma delimited text file. I have an enum for the fields to help me parse data in easier. The class that parses all the records in holds public variables for each field, and of course their variable types. I need to get the type of these variables based on the enum given.
public enum DatabaseField : int
{
NumID1 = 1,
NumID2 = 2,
NumID3 = 3,
};
public class DataBaseRecordInfo
{
public long NumID1 { get; set; }
public int NumID2 { get; set; }
public short NumID3 { get; set; }
public static Type GetType(DatabaseField field)
{
Type type;
switch (field)
{
case DatabaseField.NumID1:
type = typeof(long);
break;
case DatabaseField.NumID2:
type = typeof(int);
break;
case DatabaseField.NumID3:
type = typeof(short);
break;
default:
type = typeof(int);
break;
}
return type;
}
};
NumID1, NumID2, NumID3 all get assigned within my constructor. However, I want to get these types without ever creating an instance of DataBaseRecordInfo. Right now my static method above would work, however, if I wanted to change the variable type, I would have to change it in 2 places. Is there a way to get around having to change this in both places and keep it as a static method?
If the name is always going to match exactly you can do this using reflection.
return typeof(DataBaseRecordInfo)
.GetProperty(field.ToString(), BindingFlags.Public | BindingFlags.Instance)
.PropertyType;
You could even cache these values in a dictionary, so if found, just return the dictionary entry, otherwise determine using reflection and cache the result.
Yes, you can use the names in the enum together with reflection on the DatabaseRecordInfo type to get the types you need.
This could be done like this:
public class DataBaseRecordInfo
{
public long NumID1 { get; set; }
public int NumID2 { get; set; }
public short NumID3 { get; set; }
public static Type GetType(DatabaseField field)
{
string name = field.ToString();
Type recordType = typeof (DataBaseRecordInfo);
var props = recordType.GetProperties();
var matchedProperty = props.Where(p => name == p.Name).FirstOrDefault();
if (matchedProperty == null)
return null; // We do not have a matching property.
return matchedProperty.PropertyType;
}
};
You will probably want to cache the result in a dictionary, since the reflection can be expensive performance-wise.
How about something like this:
public static Type GetType(DatabaseField field)
{
DataBaseRecordInfo dbri = new DataBaseRecordInfo();
switch (field)
{
case DatabaseField.NumID1:
return dbri.NumID1.GetType();
case DatabaseField.NumID2:
return dbri.NumID2.GetType();
case DatabaseField.NumID3:
return dbri.NumID3.GetType();
default:
return typeof(int);
}
}
I know you said without ever having to create an instance of DataBaseRecordInfo but I'm assuming you meant an instance outside of the static method. No one ever sees this instance.
If you want to bind enum value with some additional information you can use your own CustomAttribute.
Maybe you need something like this:
public class DatabaseTypeAttribute : Attribute
{
public DatabaseTypeAttribute(Type type)
{
Type = type;
}
public Type Type { get; private set; }
}
public enum DatabaseField : int
{
[DatabaseType(typeof(long))]
NumID1 = 1,
[DatabaseType(typeof(int))]
NumID2 = 2,
[DatabaseType(typeof(short))]
NumID3 = 3,
NumID4 = 4,
};
public static class DatabaseFieldHelper
{
public static Type GetDatabaseType(this DatabaseField field)
{
var attributes = (DatabaseTypeAttribute[])typeof(DatabaseField).GetField(Enum.GetName(typeof(DatabaseField), field))
.GetCustomAttributes(typeof(DatabaseTypeAttribute), false);
if (attributes.Length == 0)
return typeof(int); //returns default type
return attributes[0].Type;
}
}
//prints: NumID1 database type: System.Int64
Console.WriteLine("NumID1 database type: {0}", DatabaseField.NumID1.GetDatabaseType());
//prints: NumID2 database type: System.Int32
Console.WriteLine("NumID2 database type: {0}", DatabaseField.NumID2.GetDatabaseType());
//prints: NumID3 database type: System.Int16
Console.WriteLine("NumID3 database type: {0}", DatabaseField.NumID3.GetDatabaseType());
//prints: NumID4 database type: System.Int32
Console.WriteLine("NumID4 database type: {0}", DatabaseField.NumID4.GetDatabaseType());

Enum ToString with user friendly strings

My enum consists of the following values:
private enum PublishStatusses{
NotCompleted,
Completed,
Error
};
I want to be able to output these values in a user friendly way though.
I don't need to be able to go from string to value again.
I use the Description attribute from the System.ComponentModel namespace. Simply decorate the enum:
private enum PublishStatusValue
{
[Description("Not Completed")]
NotCompleted,
Completed,
Error
};
Then use this code to retrieve it:
public static string GetDescription<T>(this T enumerationValue)
where T : struct
{
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(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
//Pull out the description value
return ((DescriptionAttribute)attrs[0]).Description;
}
}
//If we have no description attribute, just return the ToString of the enum
return enumerationValue.ToString();
}
I do this with extension methods:
public enum ErrorLevel
{
None,
Low,
High,
SoylentGreen
}
public static class ErrorLevelExtensions
{
public static string ToFriendlyString(this ErrorLevel me)
{
switch(me)
{
case ErrorLevel.None:
return "Everything is OK";
case ErrorLevel.Low:
return "SNAFU, if you know what I mean.";
case ErrorLevel.High:
return "Reaching TARFU levels";
case ErrorLevel.SoylentGreen:
return "ITS PEOPLE!!!!";
default:
return "Get your damn dirty hands off me you FILTHY APE!";
}
}
}
Maybe I'm missing something, but what's wrong with Enum.GetName?
public string GetName(PublishStatusses value)
{
return Enum.GetName(typeof(PublishStatusses), value)
}
edit: for user-friendly strings, you need to go through a .resource to get internationalisation/localisation done, and it would arguably be better to use a fixed key based on the enum key than a decorator attribute on the same.
I created a reverse extension method to convert the description back into an enum value:
public static T ToEnumValue<T>(this string enumerationDescription) where T : struct
{
var type = typeof(T);
if (!type.IsEnum)
throw new ArgumentException("ToEnumValue<T>(): Must be of enum type", "T");
foreach (object val in System.Enum.GetValues(type))
if (val.GetDescription<T>() == enumerationDescription)
return (T)val;
throw new ArgumentException("ToEnumValue<T>(): Invalid description for enum " + type.Name, "enumerationDescription");
}
The easiest solution here is to use a custom extension method (in .NET 3.5 at least - you can just convert it into a static helper method for earlier framework versions).
public static string ToCustomString(this PublishStatusses value)
{
switch(value)
{
// Return string depending on value.
}
return null;
}
I am assuming here that you want to return something other than the actual name of the enum value (which you can get by simply calling ToString).
That other post is Java. You can't put methods in Enums in C#.
just do something like this:
PublishStatusses status = ...
String s = status.ToString();
If you want to use different display values for your enum values, you could use Attributes and Reflection.
Some other more primitive options that avoid classes/reference types:
Array method
Nested struct method
Array method
private struct PublishStatusses
{
public static string[] Desc = {
"Not Completed",
"Completed",
"Error"
};
public enum Id
{
NotCompleted = 0,
Completed,
Error
};
}
Usage
string desc = PublishStatusses.Desc[(int)PublishStatusses.Id.Completed];
Nested struct method
private struct PublishStatusses
{
public struct NotCompleted
{
public const int Id = 0;
public const string Desc = "Not Completed";
}
public struct Completed
{
public const int Id = 1;
public const string Desc = "Completed";
}
public struct Error
{
public const int Id = 2;
public const string Desc = "Error";
}
}
Usage
int id = PublishStatusses.NotCompleted.Id;
string desc = PublishStatusses.NotCompleted.Desc;
Update (03/09/2018)
A hybrid of Extension Methods and the first technique above.
I prefer enums to be defined where they "belong" (closest to their source of origin and not in some common, global namespace).
namespace ViewModels
{
public class RecordVM
{
//public enum Enum { Minutes, Hours }
public struct Enum
{
public enum Id { Minutes, Hours }
public static string[] Name = { "Minute(s)", "Hour(s)" };
}
}
}
The extension method seems suited for a common area, and the "localized" definition of the enum now makes the extension method more verbose.
namespace Common
{
public static class EnumExtensions
{
public static string Name(this RecordVM.Enum.Id id)
{
return RecordVM.Enum.Name[(int)id];
}
}
}
A usage example of the enum and it's extension method.
namespace Views
{
public class RecordView
{
private RecordDataFieldList<string, string> _fieldUnit;
public RecordView()
{
_fieldUnit.List = new IdValueList<string, string>
{
new ListItem<string>((int)RecordVM.Enum.Id.Minutes, RecordVM.Enum.Id.Minutes.Name()),
new ListItem<string>((int)RecordVM.Enum.Id.Hours, RecordVM.Enum.Id.Hours.Name())
};
}
private void Update()
{
RecordVM.Enum.Id eId = DetermineUnit();
_fieldUnit.Input.Text = _fieldUnit.List.SetSelected((int)eId).Value;
}
}
}
Note: I actually decided to eliminate the Enum wrapper (and Name array), since it's best that the name strings come from a resource (ie config file or DB) instead of being hard-coded, and because I ended up putting the extension method in the ViewModels namespace (just in a different, "CommonVM.cs" file). Plus the whole .Id thing becomes distracting and cumbersome.
namespace ViewModels
{
public class RecordVM
{
public enum Enum { Minutes, Hours }
//public struct Enum
//{
// public enum Id { Minutes, Hours }
// public static string[] Name = { "Minute(s)", "Hour(s)" };
//}
}
}
CommonVM.cs
//namespace Common
namespace ViewModels
{
public static class EnumExtensions
{
public static string Name(this RecordVM.Enum id)
{
//return RecordVM.Enum.Name[(int)id];
switch (id)
{
case RecordVM.Enum.Minutes: return "Minute(s)";
case RecordVM.Enum.Hours: return "Hour(s)";
default: return null;
}
}
}
}
A usage example of the enum and it's extension method.
namespace Views
{
public class RecordView
{
private RecordDataFieldList<string, string> _fieldUnit
public RecordView()
{
_fieldUnit.List = new IdValueList<string, string>
{
new ListItem<string>((int)RecordVM.Enum.Id.Minutes, RecordVM.Enum.Id.Minutes.Name()),
new ListItem<string>((int)RecordVM.Enum.Id.Hours, RecordVM.Enum.Id.Hours.Name())
};
}
private void Update()
{
RecordVM.Enum eId = DetermineUnit();
_fieldUnit.Input.Text = _fieldUnit.List.SetSelected((int)eId).Value;
}
}
}
The simplest way is just to include this extension class into your project, it will work with any enum in the project:
public static class EnumExtensions
{
public static string ToFriendlyString(this Enum code)
{
return Enum.GetName(code.GetType(), code);
}
}
Usage:
enum ExampleEnum
{
Demo = 0,
Test = 1,
Live = 2
}
...
ExampleEnum ee = ExampleEnum.Live;
Console.WriteLine(ee.ToFriendlyString());
You can use Humanizer package with Humanize Enums possiblity. An eaxample:
enum PublishStatusses
{
[Description("Custom description")]
NotCompleted,
AlmostCompleted,
Error
};
then you can use Humanize extension method on enum directly:
var st1 = PublishStatusses.NotCompleted;
var str1 = st1.Humanize(); // will result in Custom description
var st2 = PublishStatusses.AlmostCompleted;
var str2 = st2.Humanize(); // will result in Almost completed (calculated automaticaly)
public enum MyEnum
{
[Description("Option One")]
Option_One
}
public static string ToDescriptionString(this Enum This)
{
Type type = This.GetType();
string name = Enum.GetName(type, This);
MemberInfo member = type.GetMembers()
.Where(w => w.Name == name)
.FirstOrDefault();
DescriptionAttribute attribute = member != null
? member.GetCustomAttributes(true)
.Where(w => w.GetType() == typeof(DescriptionAttribute))
.FirstOrDefault() as DescriptionAttribute
: null;
return attribute != null ? attribute.Description : name;
}
With respect to Ray Booysen, there is a bug in the code: Enum ToString with user friendly strings
You need to account for multiple attributes on the enum values.
public static string GetDescription<T>(this object enumerationValue)
where T : struct
{
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(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0 && attrs.Where(t => t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault() != null)
{
//Pull out the description value
return ((DescriptionAttribute)attrs.Where(t=>t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault()).Description;
}
}
//If we have no description attribute, just return the ToString of the enum
return enumerationValue.ToString();
Instead of using an enum use a static class.
replace
private enum PublishStatuses{
NotCompleted,
Completed,
Error
};
with
private static class PublishStatuses{
public static readonly string NotCompleted = "Not Completed";
public static readonly string Completed = "Completed";
public static readonly string Error = "Error";
};
it will be used like this
PublishStatuses.NotCompleted; // "Not Completed"
Issue using the top "extension method" solutions:
A private enum is often used inside another class. The extension method solution is not valid there since it must be in it's own class. This solution can be private and embedded in another class.
Clean summary of the above suggestions with sample:
namespace EnumExtensions {
using System;
using System.Reflection;
public class TextAttribute : Attribute {
public string Text;
public TextAttribute( string text ) {
Text = text;
}//ctor
}// class TextAttribute
public static class EnumExtender {
public static string ToText( this Enum enumeration ) {
MemberInfo[] memberInfo = enumeration.GetType().GetMember( enumeration.ToString() );
if ( memberInfo != null && memberInfo.Length > 0 ) {
object[] attributes = memberInfo[ 0 ].GetCustomAttributes( typeof(TextAttribute), false );
if ( attributes != null && attributes.Length > 0 ) {
return ( (TextAttribute)attributes[ 0 ] ).Text;
}
}//if
return enumeration.ToString();
}//ToText
}//class EnumExtender
}//namespace
USAGE:
using System;
using EnumExtensions;
class Program {
public enum Appearance {
[Text( "left-handed" ) ]
Left,
[Text( "right-handed" ) ]
Right,
}//enum
static void Main( string[] args ) {
var appearance = Appearance.Left;
Console.WriteLine( appearance.ToText() );
}//Main
}//class
Use Enum.GetName
From the above link...
using System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
}
}
// The example displays the following output:
// The 4th value of the Colors Enum is Yellow
// The 4th value of the Styles Enum is Corduroy
According to this documentation: https://learn.microsoft.com/pt-br/dotnet/api/system.enum.tostring?view=netframework-4.8
It is possible to just convert a enumerator to string using a format like this:
public enum Example
{
Example1,
Example2
}
Console.WriteLine(Example.Example1.ToString("g"));
//Outputs: "Example1"
You can see all the possible formats in this link: https://learn.microsoft.com/pt-br/dotnet/api/system.string?view=netframework-4.8
I happen to be a VB.NET fan, so here's my version, combining the DescriptionAttribute method with an extension method. First, the results:
Imports System.ComponentModel ' For <Description>
Module Module1
''' <summary>
''' An Enum type with three values and descriptions
''' </summary>
Public Enum EnumType
<Description("One")>
V1 = 1
' This one has no description
V2 = 2
<Description("Three")>
V3 = 3
End Enum
Sub Main()
' Description method is an extension in EnumExtensions
For Each v As EnumType In [Enum].GetValues(GetType(EnumType))
Console.WriteLine("Enum {0} has value {1} and description {2}",
v,
CInt(v),
v.Description
)
Next
' Output:
' Enum V1 has value 1 and description One
' Enum V2 has value 2 and description V2
' Enum V3 has value 3 and description Three
End Sub
End Module
Basic stuff: an enum called EnumType with three values V1, V2 and V3. The "magic" happens in the Console.WriteLine call in Sub Main(), where the last argument is simply v.Description. This returns "One" for V1, "V2" for V2, and "Three" for V3. This Description-method is in fact an extension method, defined in another module called EnumExtensions:
Option Strict On
Option Explicit On
Option Infer Off
Imports System.Runtime.CompilerServices
Imports System.Reflection
Imports System.ComponentModel
Module EnumExtensions
Private _Descriptions As New Dictionary(Of String, String)
''' <summary>
''' This extension method adds a Description method
''' to all enum members. The result of the method is the
''' value of the Description attribute if present, else
''' the normal ToString() representation of the enum value.
''' </summary>
<Extension>
Public Function Description(e As [Enum]) As String
' Get the type of the enum
Dim enumType As Type = e.GetType()
' Get the name of the enum value
Dim name As String = e.ToString()
' Construct a full name for this enum value
Dim fullName As String = enumType.FullName + "." + name
' See if we have looked it up earlier
Dim enumDescription As String = Nothing
If _Descriptions.TryGetValue(fullName, enumDescription) Then
' Yes we have - return previous value
Return enumDescription
End If
' Find the value of the Description attribute on this enum value
Dim members As MemberInfo() = enumType.GetMember(name)
If members IsNot Nothing AndAlso members.Length > 0 Then
Dim descriptions() As Object = members(0).GetCustomAttributes(GetType(DescriptionAttribute), False)
If descriptions IsNot Nothing AndAlso descriptions.Length > 0 Then
' Set name to description found
name = DirectCast(descriptions(0), DescriptionAttribute).Description
End If
End If
' Save the name in the dictionary:
_Descriptions.Add(fullName, name)
' Return the name
Return name
End Function
End Module
Because looking up description attributes using Reflection is slow, the lookups are also cached in a private Dictionary, that is populated on demand.
(Sorry for the VB.NET solution - it should be relatively straighforward to translate it to C#, and my C# is rusty on new subjects like extensions)
Even cleaner summary:
using System;
using System.Reflection;
public class TextAttribute : Attribute
{
public string Text;
public TextAttribute(string text)
{
Text = text;
}
}
public static class EnumExtender
{
public static string ToText(this Enum enumeration)
{
var memberInfo = enumeration.GetType().GetMember(enumeration.ToString());
if (memberInfo.Length <= 0) return enumeration.ToString();
var attributes = memberInfo[0].GetCustomAttributes(typeof(TextAttribute), false);
return attributes.Length > 0 ? ((TextAttribute)attributes[0]).Text : enumeration.ToString();
}
}
Same usage as underscore describes.
In case you just want to add a whitespace between the words, it is as simple as
string res = Regex.Replace(PublishStatusses.NotCompleted, "[A-Z]", " $0").Trim();
This is an update to Ray Booysen's code that uses the generic GetCustomAttributes method and LINQ to make things a bit tidier.
/// <summary>
/// Gets the value of the <see cref="T:System.ComponentModel.DescriptionAttribute"/> on an struct, including enums.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="enumerationValue">A value of type <see cref="T:System.Enum"/></param>
/// <returns>If the struct has a Description attribute, this method returns the description. Otherwise it just calls ToString() on the struct.</returns>
/// <remarks>Based on http://stackoverflow.com/questions/479410/enum-tostring/479417#479417, but useful for any struct.</remarks>
public static string GetDescription<T>(this T enumerationValue) where T : struct
{
return enumerationValue.GetType().GetMember(enumerationValue.ToString())
.SelectMany(mi => mi.GetCustomAttributes<DescriptionAttribute>(false),
(mi, ca) => ca.Description)
.FirstOrDefault() ?? enumerationValue.ToString();
}
I'm 7 years late for the party :-) But I'm sure this topic is visited frequently. So I wanted to add a little sugar to the coffee:
What about the "F" format string specifier?
PublishStatusses[] ps = Enum.GetValues<PublishStatusses>();
ps.ToList().ForEach(c => Console.Write($"{c:F} "));
There is no need for any explicit function call.
In fact there isn't even need for any format specifier.
In case of a variable assignment to a string, ToString() does the work:
string foo = PublishStatusses.Error.ToString(); // or ToString("F")
And if it is about to insert spaces between words of a CamelCase string, you can just use a regular expression:
Regex.Replace(foo, "(\\B[A-Z])", " $1")
For flags enum including.
public static string Description(this Enum value)
{
Type type = value.GetType();
List<string> res = new List<string>();
var arrValue = value.ToString().Split(',').Select(v=>v.Trim());
foreach (string strValue in arrValue)
{
MemberInfo[] memberInfo = type.GetMember(strValue);
if (memberInfo != null && memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0 && attrs.Where(t => t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault() != null)
{
res.Add(((DescriptionAttribute)attrs.Where(t => t.GetType() == typeof(DescriptionAttribute)).FirstOrDefault()).Description);
}
else
res.Add(strValue);
}
else
res.Add(strValue);
}
return res.Aggregate((s,v)=>s+", "+v);
}
Just use a static class that simulate an enum:
public static class PublishStatusses{
public const string NotCompleted = "Not Completed";
public const string Completed = "Completed";
public const string Error = "Error"
};
And to access the values, just use like an enum:
PublishStatusses.NotCompleted;
I use a generic class to store the enum/description pairs and a nested helper class to get the description.
The enum:
enum Status { Success, Fail, Pending }
The generic class:
Note: Since a generic class cannot be constrained by an enum I am constraining by struct instead and checking for enum in the constructor.
public class EnumX<T> where T : struct
{
public T Code { get; set; }
public string Description { get; set; }
public EnumX(T code, string desc)
{
if (!typeof(T).IsEnum) throw new NotImplementedException();
Code = code;
Description = desc;
}
public class Helper
{
private List<EnumX<T>> codes;
public Helper(List<EnumX<T>> codes)
{
this.codes = codes;
}
public string GetDescription(T code)
{
EnumX<T> e = codes.Where(c => c.Code.Equals(code)).FirstOrDefault();
return e is null ? "Undefined" : e.Description;
}
}
}
Usage:
EnumX<Status>.Helper StatusCodes = new EnumX<Status>.Helper(new List<EnumX<Status>>()
{
new EnumX<Status>(Status.Success,"Operation was successful"),
new EnumX<Status>(Status.Fail,"Operation failed"),
new EnumX<Status>(Status.Pending,"Operation not complete. Please wait...")
});
Console.WriteLine(StatusCodes.GetDescription(Status.Pending));
I think the best (and easiest) way to solve your problem is to write an Extension-Method for your enum:
public static string GetUserFriendlyString(this PublishStatusses status)
{
}
If you want something completely customizable, try out my solution here:
http://www.kevinwilliampang.com/post/Mapping-Enums-To-Strings-and-Strings-to-Enums-in-NET.aspx
Basically, the post outlines how to attach Description attributes to each of your enums and provides a generic way to map from enum to description.

Categories