Get Enum from Enum Attribute

Getting attributes of Enum's value

This should do what you need.

try
{
var enumType = typeof(FunkyAttributesEnum);
var memberInfos =
enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString());
var enumValueMemberInfo = memberInfos.FirstOrDefault(m =>
m.DeclaringType == enumType);
var valueAttributes =
enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = ((DescriptionAttribute)valueAttributes[0]).Description;
}
catch
{
return FunkyAttributesEnum.NameWithoutSpaces1.ToString()
}

Get enum from enum attribute

Here's a helper method that should point you in the right direction.

protected Als GetEnumByStringValueAttribute(string value)
{
Type enumType = typeof(Als);
foreach (Enum val in Enum.GetValues(enumType))
{
FieldInfo fi = enumType.GetField(val.ToString());
StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes(
typeof(StringValueAttribute), false);
StringValueAttribute attr = attributes[0];
if (attr.Value == value)
{
return (Als)val;
}
}
throw new ArgumentException("The value '" + value + "' is not supported.");
}

And to call it, just do the following:

Als result = this.GetEnumByStringValueAttribute<Als>(ComboBox.SelectedValue);

This probably isn't the best solution though as it's tied to Als and you'll probably want to make this code re-usable. What you'll probably want to strip out the code from my solution to return you the attribute value and then just use Enum.Parse as you are doing in your question.

How to get the enum type by its attribute?

All you need to do is add a default case so the method always returns something or throws an exception:

AreaCode area(int n){
switch (n) {
case 7927: return AreaCode.area1;
case 7928: return AreaCode.area2;
case 7929: return AreaCode.area3;
default: return null;
}
}

or perhaps better

AreaCode area(int n){
switch (n) {
case 7927: return AreaCode.area1;
case 7928: return AreaCode.area2;
case 7929: return AreaCode.area3;
default: throw new IllegalArgumentException(String.valueOf(n));
}
}

How to get a custom attribute on enum by passing in the enum value and attribute type?

Something like this?

using System;
using System.ComponentModel;
using System.Reflection;

namespace ConsoleApp1
{
internal static class Program
{
private static void Main(string[] args)
{
var descriptionAttribute = MyEnum.A.GetAttribute<DescriptionAttribute>();
}
}

public static class EnumExtensions
{
public static T GetAttribute<T>(this Enum @enum) where T : Attribute
{
var type = @enum.GetType();
var name = Enum.GetName(type, @enum);
var field = type.GetField(name);
var attribute = field.GetCustomAttribute<T>();
return attribute;
}
}

public enum MyEnum
{
[Description("A")] A,
[Description("B")] B
}
}

Python Enum: How to get enum values with multiple attributes

The correct syntax is B(('Val', 'One')), passing the value of the enum directly (thus in this case a tuple), or simply naming the enum variant by name: B.ValOne.

I must admit this is confusing, with __init__ automagically destructuring the tuple into two arguments. The error isn't helpful either.

Get enum description attribute from enum in c# using f# extension

I can't get it to work either, but you can rewrite the F# method to:

[<Extension>]
type Extensions =
[<Extension>]
static member inline GetEnumDescription(enum:'TEnum when 'TEnum :> Enum) : string =

let attributes = enum.GetType().GetField(enum.ToString()).GetCustomAttributes(typeof<DescriptionAttribute>, false)
match attributes.Length with
| x when x > 0 -> attributes.[0] |> (fun a -> a :?> DescriptionAttribute) |> fun a -> a.Description
| _ -> raise (InvalidOperationException("DescriptionAttribute is missing"))

You'll then get the Description property from the DescriptionAttributes when calling from C#:

  string result = Service.Unknown.GetEnumDescription();
Console.WriteLine(result);

Get Enum from Description attribute

public static class EnumEx
{
public static T GetValueFromDescription<T>(string description) where T : Enum
{
foreach(var field in typeof(T).GetFields())
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}

throw new ArgumentException("Not found.", nameof(description));
// Or return default(T);
}
}

Usage:

var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");

c# How to get enum from custom attribute?

There is another method to do this with generics:

public static T GetAttribute<T>(Enum enumValue) where T: Attribute
{
T attribute;

MemberInfo memberInfo = enumValue.GetType().GetMember(enumValue.ToString())
.FirstOrDefault();

if (memberInfo != null)
{
attribute = (T) memberInfo.GetCustomAttributes(typeof (T), false).FirstOrDefault();
return attribute;
}
return null;
}


Related Topics



Leave a reply



Submit