示例#1
0
文件: MEDMObj.cs 项目: PANKOVAN/TEST
        public static object GetTypeValue(Type pt, object value)
        {
            TypeInfo ti = pt.GetTypeInfo();

            if (pt == typeof(bool))
            {
                if (!(value is bool))
                {
                    value = (value.ToString() == "1" || value.ToString().ToLower() == "true");
                }
            }
            else if (pt == typeof(short))
            {
                if (!(value is short))
                {
                    try
                    {
                        value = Convert.ToInt16(value);
                    }
                    catch
                    {
                        value = default(short);
                    }
                }
            }
            else if (pt == typeof(int))
            {
                if (!(value is int))
                {
                    try
                    {
                        value = Convert.ToInt32(value);
                    }
                    catch
                    {
                        value = default(int);
                    }
                }
            }
            else if (pt == typeof(long))
            {
                if (!(value is long))
                {
                    try
                    {
                        value = Convert.ToInt64(value);
                    }
                    catch
                    {
                        value = default(long);
                    }
                }
            }
            else if (pt == typeof(byte))
            {
                if (!(value is byte))
                {
                    try
                    {
                        value = Convert.ToByte(value);
                    }
                    catch
                    {
                        value = default(byte);
                    }
                }
            }
            else if (pt == typeof(double))
            {
                if (!(value is double))
                {
                    try
                    {
                        value = MFunc.StringToDouble(value.ToString());
                    }
                    catch
                    {
                        value = default(double);
                    }
                }
            }
            else if (pt == typeof(decimal))
            {
                if (!(value is decimal))
                {
                    try
                    {
                        value = MFunc.StringToDecimal(value.ToString());
                    }
                    catch
                    {
                        value = default(decimal);
                    }
                }
            }
            else if (pt == typeof(DateTime))
            {
                if (!(value is DateTime))
                {
                    try
                    {
                        value = Convert.ToDateTime(value);
                    }
                    catch
                    {
                        value = default(DateTime);
                    }
                }
                if ((DateTime)value < NULLDateTime1)
                {
                    value = NULLDateTime;
                }
            }
            else if (pt == typeof(string))
            {
                if (!(value is string))
                {
                    value = value.ToString();
                }
            }
            else if (pt == typeof(Guid))
            {
                if (!(value is Guid))
                {
                    try
                    {
                        value = new Guid(value.ToString());
                    }
                    catch
                    {
                        value = default(Guid);
                    }
                }
            }
            else if (ti.IsEnum)
            {
                value = Enum.Parse(pt, value.ToString(), true);
            }
            else
            {
                value = null;
            }
            return(value);
        }