Пример #1
0
        public static object ToEnum(Type type, object value, object defaultValue, bool ignoreCase = true)
        {
            object result = null;

            if (value != null)
            {
                if (value is string)
                {
                    try
                    {
                        result = Enum.Parse(type, (string)value, ignoreCase);

                        return(result);
                    }
                    catch
                    {
                        result = defaultValue;
                    }
                }

                result = SafeClrConvert.ToInt(value).ToEnum(type, defaultValue);
            }

            return(result);
        }
Пример #2
0
        private string compressResult(string result)
        {
            var buffer_length = SafeClrConvert.ToInt(CallContext?.EngineService?.Settings["Stream_Buffer_Length"]);

            return(compressor.Base64Compress(result, new ZlibCompressionOptions {
                CompressBufferSize = buffer_length
            }));
        }
Пример #3
0
        protected virtual LogItem Transform(IDataReader reader)
        {
            var result = new LogItem
            {
                Row      = SafeClrConvert.ToLong(reader["Row"]),
                Id       = SafeClrConvert.ToInt(reader["Id"]),
                Member   = SafeClrConvert.ToString(reader["Member"]),
                Category = SafeClrConvert.ToString(reader["Category"]),
                Message  = SafeClrConvert.ToString(reader["Message"]),
                FilePath = SafeClrConvert.ToString(reader["FilePath"]),
                LogDate  = SafeClrConvert.ToDateTime(reader["LogDate"]),
                Line     = SafeClrConvert.ToInt(reader["Line"]),
            };

            return(result);
        }
Пример #4
0
        public static T AppSetting <T>(string key, T defaultValue = default(T)) where T : struct
        {
            var result = default(T);
            var type   = typeof(T);
            var value  = ConfigurationManager.AppSettings[key];

            if (type.IsEnum)
            {
                if (value.IsNumeric())
                {
                    result = SafeClrConvert.ToInt(value).ToEnum(defaultValue);
                }
                else
                {
                    if (!Enum.TryParse(value, out result))
                    {
                        result = defaultValue;
                    }
                }
            }
            else
            {
                if (type.IsBasicType())
                {
                    do
                    {
                        if (type == TypeHelper.TypeOfBool)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToBoolean(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt16)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt16(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt32)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt32(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfInt64)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt64(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfByte)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToByte(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfSByte)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToSByte(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt16)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt16(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt32)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt32(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfUInt64)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt64(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfChar)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToChar(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDateTime)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDateTime(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDecimal)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDecimal(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfDouble)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToDouble(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfFloat)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToSingle(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfGuid)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToGuid(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfTimeSpan)
                        {
                            result = (T)System.Convert.ChangeType(SafeClrConvert.ToTimeSpan(value), type);
                            break;
                        }
                        if (type == TypeHelper.TypeOfString)
                        {
                            result = (T)((object)value);
                            break;
                        }
                    }while (false);
                }
            }

            return(result);
        }
Пример #5
0
        public virtual LoggerGetPageResponse GetPage(LoggerGetPageRequest request)
        {
            var response = new LoggerGetPageResponse();

            if (ConnectionStringProvider != null)
            {
                try
                {
                    var query = GetPagingSql();

                    var constr = ConnectionStringProvider.GetConnectionString();

                    if (!string.IsNullOrEmpty(constr))
                    {
                        using (var con = new SqlConnection(constr))
                        {
                            using (var cmd = new SqlCommand(query, con))
                            {
                                cmd.CommandType = CommandType.StoredProcedure;

                                cmd.Parameters.AddInputOutput("@CurrentPage", request.CurrentPage);
                                cmd.Parameters.AddInputOutput("@PageSize", request.PageSize);
                                cmd.Parameters.AddOutput("@RecordCount", SqlDbType.Int);
                                cmd.Parameters.AddOutput("@PageCount", SqlDbType.Int);
                                cmd.Parameters.AddInput("@Member", request.Member);
                                cmd.Parameters.AddInput("@Category", request.Category);
                                cmd.Parameters.AddInput("@Message", request.Message);
                                cmd.Parameters.AddInput("@FromDate", request.FromDate);
                                cmd.Parameters.AddInput("@ToDate", request.ToDate);
                                cmd.Parameters.AddInput("@OrderBy", request.OrderBy);
                                cmd.Parameters.AddInput("@OrderDir", request.OrderDir);

                                FinalizePagingCommand(cmd);

                                con.Open();
                                var reader = cmd.ExecuteReader();

                                response.Data.CurrentPage = SafeClrConvert.ToInt(cmd.Parameters[0].Value);
                                response.Data.PageSize    = SafeClrConvert.ToInt(cmd.Parameters[1].Value);
                                response.Data.RecordCount = SafeClrConvert.ToLong(cmd.Parameters[2].Value);
                                response.Data.PageCount   = SafeClrConvert.ToInt(cmd.Parameters[3].Value);
                                response.Data.Items       = new List <LogItem>();

                                while (reader.Read())
                                {
                                    var item = Transform(reader);

                                    response.Data.Items.Add(item);
                                }
                            }
                        }

                        response.Succeeded();
                    }
                    else
                    {
                        response.Message = "ConnectionStringProvider has no connection string";
                        response.Info    = $"Provider = {ConnectionStringProvider.GetType().Name}";
                        response.SetStatus("NoConStr");
                    }
                }
                catch (Exception e)
                {
                    response.Failed(e);
                }
            }
            else
            {
                response.Message = "No ConnectionStringProvider is specified";
                response.SetStatus("NoConStrProvider");
            }

            return(response);
        }
Пример #6
0
 public static int ToInt(this string s, int @default = default(int))
 {
     return(SafeClrConvert.ToInt(s, @default));
 }