Reputation: 4342
Can I do this ? It doesn't seem so.
public enum Options
{
[Display(Name = string.Format("{0} - {1}","Option One", MyClass.myVariable))]
OptionOne=1,
[Display(Name = string.Format("{0} - {1}","Option Two", MyClass.myVariable))]
OptionTwo=2
}
As opposed to this
public enum Options
{
[Display(Name = "Option 1")]
OptionOne=1,
[Display(Name = "Option 2")]
OptionTwo=2
}
If not, how can I make the Display Name for an enum variable ?
Upvotes: 0
Views: 262
Reputation: 5705
Seems like nobody's dealing with:
If not, how can I make the Display Name for an enum variable ?
I can think about some kind of enum map plus extension method which could work like this:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public enum Foo
{
One = 1,
Two = 2,
}
public static class ExtensionMethods
{
private static readonly Dictionary<Enum, string> s_EnumMap = new Dictionary<Enum, string>
{
{ Foo.One, string.Format("{0} - {1}","Option One", 1) },
{ Foo.Two, string.Format("{0} - {1}","Option Two", 2) }
};
public static String ConvertToString(this Enum eff)
{
return s_EnumMap[eff];
}
}
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(Foo.One.ConvertToString());
Console.WriteLine(Foo.Two.ConvertToString());
}
}
}
Integers 1
and 2
can be of course replaced by e.g. static variable, such as MyClass.myVariable
. If that is the way you would use this code, then keep in mind that s_EnumMap will store the values that MyClass.myVariable
variable had at the time when you first used ExtensionMethods
class (i.e. when static fields of MyClass
were getting initialized). So modifying the code like this:
public MyClass
{
public static int myVariable = 5;
}
public static class ExtensionMethods
{
private static readonly Dictionary<Enum, string> s_EnumMap = new Dictionary<Enum, string>
{
{ Foo.One, string.Format("{0} - {1}","Option One", MyClass.myVariable) },
{ Foo.Two, string.Format("{0} - {1}","Option Two", 2) }
};
public static String ConvertToString(this Enum eff)
{
return s_EnumMap[eff];
}
}
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(Foo.One.ConvertToString());
Console.WriteLine(Foo.Two.ConvertToString());
MyClass.myVariable = 100;
Console.WriteLine(Foo.One.ConvertToString());
Console.WriteLine(Foo.Two.ConvertToString());
}
}
Would result into:
Option One - 5
Option Two - 2
Option One - 5
Option Two - 2
While after commenting out the first two Console.WriteLine
s, the output would be:
Option One - 100
Option Two - 2
So if you want to dynamicaly react to changes of MyClass.myVariable
then you have to implement some logic to update s_EnumMap`, but as long as I don't know more about the goal you are trying to achieve I cannot provide a better answer.
Upvotes: 3
Reputation: 37070
You could write a separate method to get the display name, or even a small class that has an option member and a display name member. I like Michal's idea better, but since I already started writing this I figured I'd throw it out there!
public enum Option
{
OptionOne = 1,
OptionTwo = 2
}
public static string GetOptionDisplayName(Option option)
{
switch (option)
{
case Option.OptionOne:
return string.Format("{0} - {1}", "Option One", MyClass.MyProperty);
case Option.OptionTwo:
return string.Format("{0} - {1}", "Option Two", MyClass.MyProperty);
default:
return option.ToString();
}
}
public class AnotherOption
{
public Option Option { get; set; }
public string DisplayName
{
get { return GetOptionDisplayName(this.Option); }
}
}
Upvotes: 1
Reputation: 1096
The short answer no. The value within [Display....] can be known at compile time only. E.g. you can define literals, like string or enum value. string.Format() is called at run time. If possible you should call it in you other logic, using you enum. Or use code generating, e.g. with a tt template
Upvotes: 0
Reputation: 13618
What you want cannot be done. The compiler needs to know the value at compile time.
Upvotes: 0