Beispiel #1
0
        public void Create()
        {
            foreach (FieldInfo field in typeof(T).GetFields())
            {
                if (field.FieldType == typeof(Int32))
                {
                    continue;
                }

                EnumStringAttribute attr = field.GetCustomAttributes <EnumStringAttribute>().FirstOrDefault();
                string alias             = string.Empty;
                if (attr != null)
                {
                    alias = attr.Value;
                }

                IValue value = ValueFactory.Create(field.GetValue(null));

                IVariable var = new Variable()
                {
                    Name = field.Name, Alias = alias, Public = true, Reference = new ReferenceReadOnly(value)
                };
                Properties.Add(var);
            }
        }
Beispiel #2
0
 internal static bool TryGetEnumValue(string stringVal, out T result)
 {
     if (EnumValue <T> .parseCount > 11)
     {
         if (EnumValue <T> .stringToValueLookupTable == null)
         {
             Dictionary <string, T> dictionary = new Dictionary <string, T>();
             foreach (T local in Enum.GetValues(typeof(T)))
             {
                 EnumStringAttribute customAttribute = Attribute.GetCustomAttribute(local.GetType().GetField(local.ToString()), typeof(EnumStringAttribute)) as EnumStringAttribute;
                 dictionary.Add(customAttribute.Value, local);
             }
             EnumValue <T> .stringToValueLookupTable = dictionary;
         }
         return(EnumValue <T> .stringToValueLookupTable.TryGetValue(stringVal, out result));
     }
     EnumValue <T> .parseCount++;
     foreach (T local2 in Enum.GetValues(typeof(T)))
     {
         EnumStringAttribute attribute2 = Attribute.GetCustomAttribute(local2.GetType().GetField(local2.ToString()), typeof(EnumStringAttribute)) as EnumStringAttribute;
         if ((attribute2 != null) && (attribute2.Value == stringVal))
         {
             result = local2;
             return(true);
         }
     }
     result = default(T);
     return(false);
 }
Beispiel #3
0
        internal static string ToString(T enumVal)
        {
            EnumStringAttribute customAttribute = Attribute.GetCustomAttribute(enumVal.GetType().GetField(enumVal.ToString()), typeof(EnumStringAttribute)) as EnumStringAttribute;

            if (customAttribute != null)
            {
                return(customAttribute.Value);
            }
            return(string.Empty);
        }
Beispiel #4
0
 private string EnumToString(Type type)
 {
     foreach (FieldInfo field in type.GetFields().Where(x => x.GetCustomAttributes(typeof(EnumStringAttribute), false).Length > 0))
     {
         EnumStringAttribute attr = field.GetCustomAttributes <EnumStringAttribute>().First();
         if (attr.Value == _value.ToString() || field.Name == _value.ToString())
         {
             return(attr.Value);
         }
     }
     return("");
 }
Beispiel #5
0
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var result = new Dictionary <string, object>();

            if (obj == null)
            {
                return(result);
            }

            var properties = obj.GetType().GetProperties();

            foreach (var propertyInfo in properties)
            {
                //排除的属性
                bool excludedProp = propertyInfo.IsDefined(typeof(ExcludedAttribute), true);
                if (excludedProp)
                {
                    result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
                }
                else
                {
                    if (!this._jsonSetting.PropertiesToIgnore.Contains(propertyInfo.Name))
                    {
                        bool ignoreProp = propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true);
                        if ((this._jsonSetting.IgnoreNulls || ignoreProp) && propertyInfo.GetValue(obj, null) == null)
                        {
                            continue;
                        }

                        //当值匹配时需要忽略的属性
                        IgnoreValueAttribute attri = propertyInfo.GetCustomAttribute <IgnoreValueAttribute>();
                        if (attri != null && attri.Value.Equals(propertyInfo.GetValue(obj)))
                        {
                            continue;
                        }

                        EnumStringAttribute enumStringAttri = propertyInfo.GetCustomAttribute <EnumStringAttribute>();
                        if (enumStringAttri != null)
                        {   //枚举类型显示字符串
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(obj).ToString());
                        }
                        else
                        {
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #6
0
 public override string ToString()
 {
     return(EnumStringAttribute.GetStringValue(Type) + (SubType != TokenSubTypeEnum.NA ? " " + EnumStringAttribute.GetStringValue(SubType) : ""));
 }
Beispiel #7
0
 public static T Resolve <T>(Enum enumValue)
 {
     return(Resolve <T>(EnumStringAttribute.GetSearchValue(enumValue)));
 }
Beispiel #8
0
 public static void Register <TFrom, TTo>(Enum parameter) where TTo : TFrom
 {
     Current.RegisterType <TFrom, TTo>(EnumStringAttribute.GetSearchValue(parameter), new HierarchicalLifetimeManager());;
 }