コード例 #1
0
        /// <summary>
        /// Returns parameters for current task work
        /// </summary>
        /// <returns></returns>
        public T GetParamByName <T>(string name)
        {
            if (Parameters == null)
            {
                return(default(T) !);
            }

            var parameters = JsonSerializer.Deserialize <IEnumerable <WorkParameter> >(Parameters);
            var parameter  = parameters.FirstOrDefault(x => x.Name !.ToLower().Equals(name.ToLower()));

            if (parameter == null)
            {
                return(default(T) !);
            }

            var type = Type.GetType(parameter.TypeName);

            if (type !.IsPrimitive)
            {
                if (TypeHelper.CanChangeType(parameter.Value, type))
                {
                    return((T)Convert.ChangeType(parameter.Value, type));
                }
                return((T)parameter.Value);
            }

            switch (type.Name)
            {
            case "Decimal":
                return((T)Convert.ChangeType(parameter.Value, type));

            case "DateTime":
                return((T)Convert.ChangeType(parameter.Value, type));

            case "String":
                var resultString = parameter.Value.ToString();
                return((T)Convert.ChangeType(resultString !, type));

            case "Guid":
                var resultGuid = Guid.Parse(parameter.Value.ToString() !);
                return((T)Convert.ChangeType(resultGuid !, type));

            default:
                try
                {
                    return(JsonSerializer.Deserialize <T>(parameter.Value.ToString()));
                }
                catch
                {
                    throw new MicroserviceArgumentOutOfRangeException();
                }
            }
        }