Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107682
  • 博文数量: 20
  • 博客积分: 1910
  • 博客等级: 上尉
  • 技术积分: 485
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-10 15:46
文章分类

全部博文(20)

文章存档

2013年(3)

2012年(4)

2011年(10)

2010年(1)

2009年(2)

我的朋友

分类:

2010-04-16 23:03:09

Today, I spent lots of time trying to convert string to type. Here below is my finding. Convert.ChangeType() can be used in this situation. Only the following type can be converted successfully: Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime, and String.

///



    /// supported type : Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String.

    /// %28VS.71%29.aspx

    ///

    ///


    internal class StringToType
    {
        public static T FromString<T>(string text)
        {
            //return (T)Convert.ChangeType(text, typeof(T), CultureInfo.InvariantCulture);

            return (T)Convert.ChangeType(text, typeof(T));
        }

        public static T FromXmlAttribute<T>(XmlNode node, string attributeName)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            if (String.IsNullOrEmpty(attributeName))
                throw new ArgumentException("Cannot be null or empty", "attributeName");

            XmlAttribute attribute = node.Attributes[attributeName];
            if (attribute == null)
                return default(T);

            return FromString<T>(attribute.Value);
        }

        public static T FromXAttribute<T>(XElement element, string attributeName)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            if (String.IsNullOrEmpty(attributeName))
                throw new ArgumentException("Cannot be null or empty", "attributeName");

            XAttribute attribute = element.Attribute(attributeName);
            if (attribute == null)
                return default(T);

            return FromString<T>(attribute.Value);
        }
    }

 

 


Here below is a test function. 

void TestStringToType()
        {
            Console.WriteLine("String To int : {0}", StringToType.FromString<int>("32767"));
            TimeSpan timeSpan = new TimeSpan(2, 15, 45, 10, 10);
            DateTime dt = new DateTime(2010, 4, 16, 10, 24, 55);
            Console.WriteLine("String To DateTime : {0}", StringToType.FromString<DateTime>(dt.ToString()));

            XElement xe = new XElement("Root", new XAttribute("price", 35));
            Console.WriteLine("XElement : {0}", xe);
            Console.WriteLine("XElement attribute price : {0}", StringToType.FromXAttribute<int>(xe, "price"));

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("The Art of Data Structure");
            XmlNode xn = xmlDoc.SelectSingleNode("/Books/Book");
            Console.WriteLine("XmlNode : {0}", xn.OuterXml);
            Console.WriteLine("XmlNode attribute price : {0}", StringToType.FromXmlAttribute<double>(xn, "price"));
        }


阅读(431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~