20078888

技术前线

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Xml;
namespace DBUtility
{
    public class CommonHelper
    {
        public static object DeserializeModel(byte[] b, object SampleModel)
        {
            if ((b == null) || (b.Length == 0))
            {
                return SampleModel;
            }
            object obj2 = new object();
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream serializationStream = new MemoryStream();
            try
            {
                serializationStream.Write(b, 0, b.Length);
                serializationStream.Position = 0L;
                obj2 = formatter.Deserialize(serializationStream);
                serializationStream.Close();
            }
            catch
            {
            }
            return obj2;
        }

        public static bool GetBool(object obj)
        {
            return ((obj.ToString() == "1") || (obj.ToString().ToLower() == "true"));
        }

        public static byte Getbyte(object obj)
        {
            if (obj.ToString() != "")
            {
                return byte.Parse(obj.ToString());
            }
            return 0;
        }

        public static byte[] GetByte(object obj)
        {
            if (obj.ToString() != "")
            {
                return (byte[])obj;
            }
            return null;
        }

        public static DateTime GetDateTime(object obj)
        {
            if (obj.ToString() != "")
            {
                return DateTime.Parse(obj.ToString());
            }
            return DateTime.MinValue;
        }

        public static decimal GetDecimal(object obj)
        {
            if (obj.ToString() != "")
            {
                return decimal.Parse(obj.ToString());
            }
            return 0M;
        }

        public static Guid GetGuid(object obj)
        {
            if (obj.ToString() != "")
            {
                return new Guid(obj.ToString());
            }
            return Guid.Empty;
        }

        public static int GetInt(object obj)
        {
            if (obj.ToString() != "")
            {
                return int.Parse(obj.ToString());
            }
            return 0;
        }

        public static long GetLong(object obj)
        {
            if (obj.ToString() != "")
            {
                return long.Parse(obj.ToString());
            }
            return 0L;
        }

        public static string GetString(object obj)
        {
            return obj.ToString();
        }

        public static string ModelToXML(object model)
        {
            XmlDocument document = new XmlDocument();
            XmlElement newChild = document.CreateElement("Model");
            document.AppendChild(newChild);
            if (model != null)
            {
                foreach (PropertyInfo info in model.GetType().GetProperties())
                {
                    XmlElement element2 = document.CreateElement(info.Name);
                    if (info.GetValue(model, null) != null)
                    {
                        element2.InnerText = info.GetValue(model, null).ToString();
                    }
                    else
                    {
                        element2.InnerText = "[Null]";
                    }
                    newChild.AppendChild(element2);
                }
            }
            return document.OuterXml;
        }

        public static byte[] SerializeModel(object obj)
        {
            if (obj != null)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream serializationStream = new MemoryStream();
                formatter.Serialize(serializationStream, obj);
                serializationStream.Position = 0L;
                byte[] buffer = new byte[serializationStream.Length];
                serializationStream.Read(buffer, 0, buffer.Length);
                serializationStream.Close();
                return buffer;
            }
            return new byte[0];
        }

        public static object XMLToModel(string xml, object SampleModel)
        {
            if (!string.IsNullOrEmpty(xml))
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);
                XmlNodeList childNodes = document.SelectSingleNode("Model").ChildNodes;
                foreach (XmlNode node in childNodes)
                {
                    foreach (PropertyInfo info in SampleModel.GetType().GetProperties())
                    {
                        if (node.Name == info.Name)
                        {
                            if (node.InnerText != "[Null]")
                            {
                                if (info.PropertyType == typeof(Guid))
                                {
                                    info.SetValue(SampleModel, new Guid(node.InnerText), null);
                                }
                                else
                                {
                                    info.SetValue(SampleModel, Convert.ChangeType(node.InnerText, info.PropertyType), null);
                                }
                            }
                            else
                            {
                                info.SetValue(SampleModel, null, null);
                            }
                        }
                    }
                }
            }
            return SampleModel;
        }
    }
}

posted on 2010-05-07 17:32  许雪林  阅读(582)  评论(0)    收藏  举报