Created
May 2, 2018 19:25
-
-
Save teamday/7a57524924b1fde5bdfaacd0f820b79a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringValue : System.Attribute | |
{ | |
private string _value; | |
public StringValue(string value) | |
{ | |
_value = value; | |
} | |
public string Value | |
{ | |
get { return _value; } | |
} | |
public static string GetStringValue(Enum Flagvalue) | |
{ | |
Type type = Flagvalue.GetType(); | |
string[] flags = Flagvalue.ToString().Split(',').Select(x => x.Trim()).ToArray(); | |
List<string> values = new List<string>(); | |
for (int i = 0; i < flags.Length; i++) | |
{ | |
FieldInfo fi = type.GetField(flags[i].ToString()); | |
StringValue[] attrs = | |
fi.GetCustomAttributes(typeof(StringValue), | |
false) as StringValue[]; | |
if (attrs.Length > 0) | |
{ | |
values.Add(attrs[0].Value); | |
} | |
} | |
return String.Join(",", values); | |
} | |
[Flags] | |
public enum CompeteMetric | |
{ | |
/// <summary> | |
/// u | |
/// </summary> | |
[StringValue("u")]//Json mapping | |
Basic_UniqueVisitors = 1 //Basic | |
, | |
/// <summary> | |
/// vi | |
/// </summary> | |
[StringValue("vi")]//json mapping | |
Basic_Visits = 2// Basic | |
, | |
/// <summary> | |
/// rank | |
/// </summary> | |
[StringValue("rank")]//json mapping | |
Basic_Rank = 4//Basic | |
} | |
Example | |
CompeteMetric metrics = CompeteMetric.Basic_Visits | CompeteMetric.Basic_Rank; | |
string strmetrics = StringValue.GetStringValue(metrics); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment