日期:2014-05-17  浏览次数:21089 次

走进c#(类型转换的一个类Object对象转int String double byte[] Stream)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Test2
{
    class MyConvert
    {
        #region string转


        public static DateTime getDateTime(Object value)
        {
            try
            {
                if (value == null)
                {
                    return DateTime.Parse("0000/00/00 00:00:00");
                }
                switch(value.GetType().ToString())
                {
                    case "System.String":
                            return DateTime.Parse((String)value);
                    case "System.Int64":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    case "System.Int32":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    case "System.Boolean":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    case "System.Double":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    case "System.IO.MemoryStream":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    case "System.Byte[]":
                        return MyConvert.getDateTime(MyConvert.getString(value));
                    default:
                        return DateTime.Parse("0000/00/00 00:00:00");
                    
                }
              
            }
            catch(Exception) {
                return DateTime.Parse("0000/00/00 00:00:00");
            }
        }

        //string转换为Int32
        public static int getInt(Object value)
        {
            try
            {
                if (value == null)
                {
                    return 0;
                }
                switch(value.GetType().ToString())
                {
                    case "System.String":
                        return Int32.Parse((string)value);
                    case "System.Int64":
                        return Int32.Parse(((long)value).ToString());
                    case "System.Int32":
                        return (int)value;
                    case "System.Double":
                        return (int)System.Math.Round((double)value, 0);
                    case "System.Boolean":
                        return 0;
                    case "System.DateTime":
                        return 0;
                    case "System.IO.MemoryStream":
                        return MyConvert.getInt(MyConvert.getString(value));
                    case "System.Byte[]":
                        return MyConvert.getInt(MyConvert.getString(value));
                    default:
                        return 0;
                }
            }
            catch{
                return 0;
            }
        }

        //string转换为double
        public static double getDouble(Object value)
        {
            try
            {
               if(value==null)
               {
                   return 0.0;
               }
               switch (value.GetType().ToString()) 
               {
                   case "System.String":
                       return Double.Parse((string)value);
                   case "System.Int64":
                       return Double.Parse(((long)value).ToString());
                   case "System.Int32":
                       return Double.Parse(((int)value).ToString());
                   case "System.Boolean":
                       return 0.0;
                   case "System.Double":
                       return (int)System.Math.Round((double)value, 0);
                   case "System.DateTime":
                       return 0.0;
                   case "System.IO.MemoryStream":
                       return MyConvert.getInt(MyConvert.getString(value));
                   c