c# - Enum Display Name - Use variable? -
Can I do this? It does not seem so. Public enum option {[Display (name = string. Format ("{0} - {1}", "option one", MyClass.myVariable))] OptionOne = 1, [Display (Name = string format ("{0} - {1}", "option two", MyClass.myVariable))] OptionTwo = 2}
public anonymous option {[display ( Name = "Option 1")] OptionOn = 1, [Display (name = "Option2")] OptionTwo = 2}
code>
If not, So how do I create a display name for an enum variable?
It seems that no one is working with it:
If not, then How do I create a display name for an enum variable?
I can think of any type of map and extension method that can work like this:
System use; Using System.Collections.Generic; Namespace Console Application 1 {Public Enum Fu {1 = 1, 2 = 2}, Public Static Class Extension-Method {Private Static Readonly Dictionary & lt; Enum, string & gt; S_EnumMap = New Dictionary & lt; Enum, string & gt; {{Foo.One, string.Format ("{0} - {1}", "option one", 1)}, {Foo.Two, string.Format ("{0} - {1}", "option Two ", 2)}}; Public Static String Converttoastring (this ANUUF) {Return S_Numem [EPF]; }} Internal category program {Private Static Zero Main (string [] ARG) {console. WrightLine (Foo.One.ConvertToString ()); Console.WriteLine (Foo.Two.ConvertToString ()); }}}
The integers can be replaced by 1
and 2
example static variables, such as MyClass.myVariable
. If you use this code, keep in mind that s_EnumMap will store those values that the MyClass.myVariable
variable was in when you first used the ExtensionMethods
class (I.e. when steady fields of MyClass
were being started). Therefore, modifying the code as follows:
Public MyClass {public static int myVariable = 5; } Public Static Class ExtensionMathals {Private Static Readonly Dictionary & lt; Enum, string & gt; S_EnumMap = New Dictionary & lt; Enum, string & gt; {{Foo.One, string.Format ("{0} - {1}", "option one", MyClass.myVariable)}, {Foo.Two, string.Format ("{0} - {1}", "Option two", 2)}}; Public Static String Converttoastring (this ANUUF) {Return S_Numem [EPF]; }} Internal category program {Private Static Zero Main (string [] ARG) {console. WrightLine (Foo.One.ConvertToString ()); Console.WriteLine (Foo.Two.ConvertToString ()); MyClass.myVariable = 100; Console.WriteLine (Foo.One.ConvertToString ()); Console.WriteLine (Foo.Two.ConvertToString ()); }}
The result will be:
option one - 5 options - two options - 2 options one - 5
option two - 2
After commenting outside the first two console. WriteLine
, the output will be:
option one - 100 - option two - 2
So if you want to give feedback on the changes to the MyClass.myVariable
, you will need to apply some arguments to update s_EnumMap, but Until I am about that goal I do not know much in whatever you are trying to achieve, I can not give a better answer.
Comments
Post a Comment