走进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));
case "System.Byte[]":
return MyConvert.getInt(MyConvert.getString(value));
default:
return 0;
}
}
catch
{
return 0.0;
}
}
public static Stream getStream(Object value)
{
try
{
if (value == null)
{
return new MemoryStream(Encoding.Default.GetBytes("null"));
}
switch (value.GetType().ToString())
{
case "System.String":
return new MemoryStream(Encoding.Default.GetBytes((string)value));
case "System.Int64":
return new MemoryStream(Encoding.Default.GetBytes(MyConvert.getString(value)));
case "System.Int32":
return new MemoryStream(Encoding.Default.GetBytes(MyConvert.getString(value)));
case "System.Boolean":
return new MemoryStream(Encoding.Default.GetBytes(MyConvert.getString(value)));
case "System.DateTime":
return new MemoryStream(Encoding.Default.GetBytes(MyConvert.getString(value)));
case "System.Byte[]":
return new MemoryStream(Encoding.Default.GetBytes(MyConvert.getString(value)));
default:
return new MemoryStream(Encoding.Default.GetBytes("null"));
}
// myStream = new MemoryStream(Encoding.Default.GetBytes(myString));
// Console.WriteLine(new StreamReader(myStream).ReadToEnd());
// Console.WriteLine("myStream is " + myStream.Read());
}
catch{
return new MemoryStream(Encoding.Default.GetBytes("null"));
}
}
//string转换为byte[]
public static byte[] getByte(Object value)
{
try
{
if (value == null)
{
return null;
}
switch(value.GetType().ToString())
{
case "System.String":
return Encoding.Default.GetBytes((string)value);
case "System.Int64":
return Encoding.Default.GetBytes(MyConvert.getString(value));
case "System.Int32":
return Encoding.Default.GetBytes(MyConvert.getString(value));
case "System.Boolean":
return Encoding.Default.GetBytes(MyConvert.getString(value));
case "System.DateTime":
return Encoding.Default.GetBytes(MyConvert.getString(value));
case "System.IO.MemoryStream":
return Encoding.Default.GetBytes(MyConvert.getString(value));
default:
return null;
}
}
catch
{
return null;
}
}
#endregion
/*******************************************
*
*
*
* ****************************************/
#region 转string
public static string getString(Object obj)
{
switch (obj.GetType().ToString())
{
case "System.DateTime":
Console.WriteLine(((DateTime)obj).ToLongDateString());
Console.WriteLine(((DateTime)obj).ToLongDateString().GetType());
return ((DateTime)obj).ToLongDateString();
case "System.Int32":
Console.WriteLine(((Int32)obj).ToString());
Console.WriteLine(((Int32)obj).ToString().GetType());
return ((Int32)obj).ToString();
case "System.Int64":
Console.WriteLine(((Int64)obj).ToString());
Console.WriteLine(((Int64)obj).ToString().GetType());
return ((Int64)obj).ToString();
case "System.Double":
Console.WriteLine(((double)obj).ToString());
Console.WriteLine(((double)obj).ToString().GetType());
return ((double)obj).ToString();
case "System.IO.MemoryStream":
Stream s = (Stream)obj;
StreamReader sr = new StreamReader(s);
string myString=string.Empty;
while(sr.Peek()>-1)
{
string input = sr.ReadLine();
myString += input;
}
Console.WriteLine(myString);
Console.WriteLine(myString.GetType());
return myString;
case "System.Byte[]":
return Encoding.UTF8.GetString((byte[])obj);
case "System.Boolean":
if((bool)obj==false)
return "false";
else return "true";
default:
Console.WriteLine("该格式无法转换");
return null;
}
}
#endregion
}
}