Document directory
- Example
- Program description
-
- Zookeeper case
- Additional information
Sometimes, Int, bool, and so on may be written into text, such as: <%: item. Enable? "Disabled": "disabled" %>, used? : Processing is very convenient, but if you want to add more than two strings to the operation, it is very difficult to handle, but the three pages of the page <%: %>, <% = %>, and <% # %> cannot use if or switch. Because oracle is used, its decode function is very popular, so I will handle it by using a set of extension methods.
Example
<% -- Item. the type of state is int -- %> <%: item. state. decode ("unknown", "cannot be used", "waiting for success", "renew", "pending") %> <% -- item. is the type of enable bool? -- %> <%: Item. enable. decode ("disabled", "disabled", "unspecified") %> <% -- item. the type of enable is int, but the value of enable is not invalid -- %> <%: item. state. decode (new dictionary <int, string >{{ 100, "Processing Center" },{ 200, "successful" },{ 300, "lost" }}) %>
Program description
My extension method is similar to valuetype. Why does it use valuetype ?, Because valuetype is a base class of bool, Int, long, float, and even Enum (that is, struct in C #), there are multiple types of effects, the program can also be a little less frequently, and then I have the nullable response. When it is null, the program will apply the meaage, because the value may not exceed the limit, for example, 100, 200, or 300, instead of 0, 1, or 2, there are always more than four shards.
If the value is too many, you can use these two methods.
Public static string decode (this valuetype value, Params string [] messages) {// Most valuetype types can be converted to int key = convert. toint32 (value); // use the last message if (Key> = messages. length | key <0) {return messages. lastordefault ();} return messages [Key];} public static string decode <t> (this nullable <t> value, Params string [] messages) where T: struct {If (value. hasvalue) {return decode (value. value, messages);} else {// use the last message return messages if no value exists. lastordefault ();}}
You can use these two methods if the value is not valid.
Public static string decode (this valuetype value, idictionary <int, string> messages, string defaultmessage = NULL) {// Most valuetype types can be converted to int key = convert. toint32 (value); // determines whether the token has a key if (messages. containskey (key) {return messages [Key];} else {// there is a mechanism to set the message to allow users to set the message, if (defaultmessage = NULL) {return messages. values. lastordefault () ;}else {return defaultmessage ;}} public static string decode <t> (this nullable <t> value, idictionary <int, string> messages, string defaultmessage = NULL) where T: struct {If (value. hasvalue) {return decode (value. value, messages);} else {// use the last message if (defaultmessage = NULL) {return messages. values. lastordefault () ;}else {return defamessage message ;}}}
Note:
I have thought about changing the type of the method that does not parse this to object so that all objects can be used, however, I recommend that you have no requirements for other texts such as A, B, and C. If you have any requirements, modify them on your own.
Zookeeper case
enum TestEnum{ AA, BB, CC, DD}[TestMethod, Owner("Wade")]public void U__DecodeExtension_Decode(){ var defaultMessage = "Test"; var message1 = new string[] { "A", "B", "C", "D" }; var message2 = new Dictionary<int, string> { { 10, "A" }, { 20, "B" }, { 30, "C" }, { 40, "D" } }; Assert.AreEqual("B", true.Decode(message1)); Assert.AreEqual("A", false.Decode(message1)); Assert.AreEqual("A", 0.Decode(message1)); Assert.AreEqual("B", 1.Decode(message1)); Assert.AreEqual("D", 4.Decode(message1)); Assert.AreEqual("D", (-1).Decode(message1)); Assert.AreEqual("B", ((byte)1).Decode(message1)); Assert.AreEqual("D", 10.Decode(message1)); Assert.AreEqual("D", (10f).Decode(message1)); Assert.AreEqual("B", (1L).Decode(message1)); Assert.AreEqual("A", (0L).Decode(message1)); Assert.AreEqual("A", TestEnum.AA.Decode(message1)); Assert.AreEqual("B", TestEnum.BB.Decode(message1)); Assert.AreEqual("D", true.Decode(message2)); Assert.AreEqual("D", false.Decode(message2)); Assert.AreEqual("A", 10.Decode(message2)); Assert.AreEqual("B", 20.Decode(message2)); Assert.AreEqual("D", 40.Decode(message2)); Assert.AreEqual("D", (-1).Decode(message2)); Assert.AreEqual("A", (10f).Decode(message2)); Assert.AreEqual("Test", true.Decode(message2, defaultMessage)); Assert.AreEqual("Test", false.Decode(message2, defaultMessage)); Assert.AreEqual("A", 10.Decode(message2, defaultMessage)); Assert.AreEqual("B", 20.Decode(message2, defaultMessage)); Assert.AreEqual("D", 40.Decode(message2, defaultMessage)); Assert.AreEqual("Test", (-1).Decode(message2, defaultMessage)); Assert.AreEqual("A", ((byte)10).Decode(message2, defaultMessage)); Assert.AreEqual("A", (10f).Decode(message2, defaultMessage)); Assert.AreEqual("D", (new Nullable<int>()).Decode(message1)); Assert.AreEqual("A", (new Nullable<int>(0)).Decode(message1)); Assert.AreEqual("D", (new Nullable<bool>()).Decode(message1)); Assert.AreEqual("B", (new Nullable<bool>(true)).Decode(message1)); Assert.AreEqual("D", (new Nullable<int>()).Decode(message2)); Assert.AreEqual("A", (new Nullable<int>(10)).Decode(message2)); Assert.AreEqual("D", (new Nullable<int>(40)).Decode(message2));}
Original shard
Additional information
- Value types (C # reference)