コード例 #1
0
ファイル: EnumCache.cs プロジェクト: ChenKa1/Kuicker
        public static EnumRef Get(Type type)
        {
            if (!type.IsEnum)
            {
                throw new NotSupportedException(
                          LogRecord
                          .Create()
                          .SetMessage("Only enum type allowed")
                          .Add("Type", type.FullName)
                          .Error()
                          .Message
                          );
            }

            EnumRef ef;

            if (_EnumRefs.TryGetValue(type.FullName, out ef))
            {
                return(ef);
            }

            lock (_EnumRefLock) {
                if (_EnumRefs.TryGetValue(type.FullName, out ef))
                {
                    return(ef);
                }

                ef = new EnumRef()
                {
                    DefaultValue = type.GetDefaultValue().ToStringX(),
                    Description  = type.GetDescription().AirBag(type.Name),
                    Category     = type.GetCategory(),
                    FullName     = type.FullName,
                    Type         = type,
                };

                var      infos  = type.GetFields().ToList();
                string[] names  = Enum.GetNames(type);
                Array    values = Enum.GetValues(type);
                for (int i = 0; i < names.Length; i++)
                {
                    var info = infos.FirstOrDefault(x =>
                                                    x.Name == names[i]
                                                    );
                    if (null == info)
                    {
                        throw new NotSupportedException(
                                  LogRecord
                                  .Create()
                                  .SetMessage("Enum cache error")
                                  .Add("Type", type.FullName)
                                  .Error()
                                  .Message
                                  );
                    }

                    var ei = new EnumItem()
                    {
                        Category    = info.GetCategory(),
                        Description = info.GetDescription().AirBag(info.Name),
                        Type        = type,
                        Name        = info.Name,
                        Value       = (int)values.GetValue(i),
                    };

                    ef.Items.Add(ei);
                }
                _EnumRefs.Add(type.FullName, ef);

                return(ef);
            }
        }
コード例 #2
0
        internal static object NullToEmptyAndTrim(
            Type objType,
            object objValue,
            PropertyInfo propertyInfo,
            object propertyValue)
        {
            try {
                // 1. objType is null
                if (null == objType)
                {
                    return(null);
                }

                // 2. objValue is null
                if (null == objValue)
                {
                    // 2.1 ValueType
                    if (objType.IsValueType)
                    {
                        return(objValue);
                    }

                    // 2.2 String
                    if (objType.Equals(typeof(string)))
                    {
                        return(string.Empty);
                    }

                    // 2.3 CreateInstance
                    objValue = Reflector.CreateInstance(objType);
                    objValue = NullToEmptyAndTrim(
                        objType, objValue, null, null
                        );
                    return(objValue);
                }

                // 3. propertyInfo is null
                if (null == propertyInfo)
                {
                    // 3.1 ValueType
                    if (objType.IsValueType)
                    {
                        return(objValue);
                    }

                    // 3.2 String
                    if (objType.Equals(typeof(string)))
                    {
                        string v = objValue as string;
                        return(v.Trim());
                    }

                    // 3.3 Properties
                    foreach (var pi in objType.GetProperties())
                    {
                        if (!pi.CanRead || !pi.CanWrite)
                        {
                            continue;
                        }
                        object v = Reflector.GetValue(pi, objValue);
                        NullToEmptyAndTrim(objType, objValue, pi, v);
                    }
                    return(objValue);
                }

                var propertyType = propertyInfo.PropertyType;

                // 4. propertyValue is null
                if (null == propertyValue)
                {
                    // 4.1 ValueType
                    if (propertyType.IsValueType)
                    {
                        return(propertyValue);
                    }

                    // 4.2 String
                    if (propertyType.Equals(typeof(string)))
                    {
                        Reflector.SetValue(
                            objValue, propertyInfo, string.Empty
                            );
                        return(objValue);
                    }

                    // 4.3 IEnumerable
                    if (propertyType.IsDerived <IEnumerable>())
                    {
                        var v           = typeof(List <>);
                        var genericArgs = propertyType.GetGenericArguments();
                        if (null != genericArgs)
                        {
                            var concreteType = v.MakeGenericType(genericArgs);
                            var newList      = Activator.CreateInstance(concreteType);
                            Reflector.SetValue(objValue, propertyInfo, newList);
                        }
                        return(objValue);
                    }

                    // 4.4 Properties
                    object x = Reflector.CreateInstance(propertyType);
                    foreach (var pi in propertyType.GetProperties())
                    {
                        if (!pi.CanRead || !pi.CanWrite)
                        {
                            continue;
                        }
                        object v = pi.GetValue(x, new object[0]);
                        x = NullToEmptyAndTrim(propertyType, x, pi, v);
                    }
                    Reflector.SetValue(objValue, propertyInfo, x);
                    return(objValue);
                }

                // 5. all is not null
                // 5.1 ValueType
                if (propertyType.IsValueType)
                {
                    return(objValue);
                }

                // 5.2 String
                if (propertyType.Equals(typeof(string)))
                {
                    string v = propertyValue as string;
                    propertyValue = v.Trim();
                    Reflector.SetValue(objValue, propertyInfo, v);
                    return(objValue);
                }

                // 5.3 IEnumerable
                if (propertyType.IsDerived <IEnumerable>())
                {
                    var v           = typeof(List <>);
                    var genericArgs = propertyType.GetGenericArguments();
                    if (null != genericArgs)
                    {
                        var concreteType = v.MakeGenericType(genericArgs);
                        var newList      = Activator.CreateInstance(concreteType);
                        var list         = propertyValue as IEnumerable;
                        foreach (var one in list)
                        {
                            object o  = one;
                            object o2 = NullToEmptyAndTrim(
                                genericArgs[0], o, null, null
                                );
                            Reflector.Invoke("Add", newList, new { o2 });
                        }
                        Reflector.SetValue(objValue, propertyInfo, newList);
                    }
                    return(objValue);
                }

                // 5.4 Properties
                object y = propertyValue;
                foreach (var pi in propertyType.GetProperties())
                {
                    if (!pi.CanRead || !pi.CanWrite)
                    {
                        continue;
                    }
                    object v = pi.GetValue(propertyValue, new object[0]);
                    y = NullToEmptyAndTrim(propertyType, y, pi, v);
                }
                Reflector.SetValue(objValue, propertyInfo, y);
            } catch (Exception ex) {
                LogRecord.Create().Add(ex).Error();
            }

            return(objValue);
        }