如何将String类型转换成任意基本类型
展开全部
使用反编译软件Reflector打开System.Web.Mvc(直接在VS2008下右键选择Reflector打开就行了,默认位置在C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 1.0\Assemblies\System.Web.Mvc.dll)
顺着asp.net mvc的访问路径,一路到了下来。发现原来还有一个这么简单的方法,这里直接把我简单的DEMO列出来,相信大家都很容易看明白了:
using System;
using System.ComponentModel;
namespace YcoeXu.Common
{
public static class StringExtensions
{
/// <summary>
/// 将字符串格式化成指定的数据类型
/// </summary>
/// <param name="str"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Object Format(this String str, Type type)
{
if (String.IsNullOrEmpty(str))
return null;
if (type == null)
return str;
if (type.IsArray)
{
Type elementType = type.GetElementType();
String[] strs = str.Split(new char[] { ';' });
Array array = Array.CreateInstance(elementType, strs.Length);
for (int i = 0, c = strs.Length; i < c; ++i)
{
array.SetValue(ConvertSimpleType(strs[i], elementType), i);
}
return array;
}
return ConvertSimpleType(str,type);
}
private static object ConvertSimpleType(object value, Type destinationType)
{
object returnValue;
if ((value == null) || destinationType.IsInstanceOfType(value))
{
return value;
}
string str = value as string;
if ((str != null) && (str.Length == 0))
{
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool flag = converter.CanConvertFrom(value.GetType());
if (!flag)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!flag && !converter.CanConvertTo(destinationType))
{
throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
}
try
{
returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
}
catch (Exception e)
{
throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
}
return returnValue;
}
}
}
DEMO:
在配置文件里自定义配置:
1. 在<configSections></configSections>节点内添加节点:
<section name="XuConfig" type="System.Configuration.NameValueSectionHandler" />
2. 写配置看起来会是这样的:
<configSections>
//..其它代码
<section name="XuConfig" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<XuConfig>
<add key="ID" value="123"/>
<add key="Name" value="YcoeXu"/>
<add key="Roles" value="Member,Admin"/>
</XuConfig>
写个类自动加载
using System;
using System.Reflection;
using System.Collections.Specialized;
using System.Configuration;
using YcoeXu.Common;
namespace YcoeXu.Test
{
public class XuConfig
{
private XuConfig() { }
private static XuConfig config = null;
private static XuConfig Instance
{
get
{
if (config == null)
{
config = new XuConfig();
Type type = typeof(XuConfig);
//从配置文件里读取XuConfig节点
NameValueCollection xuConfig = (NameValueCollection)ConfigurationManager.GetSection("XuConfig");
//根据Key匹配对应的属性
foreach (String key in xuConfig.AllKeys)
{
PropertyInfo pi = type.GetProperty(key);
if (pi == null || String.IsNullOrEmpty(xuConfig[key]))
continue;
//自动转换类型并注入值
pi.SetValue(config, xuConfig[key].Format(pi.PropertyType), null);
}
}
return config;
}
}
public int ID { set; get; }
public String Name { set; get; }
public Role[] Roles { set; get; }
public void Test()
{
Console.WriteLine(XuConfig.Instance.Name);
Console.WriteLine(XuConfig.Instance.ID);
foreach (Role r in XuConfig.Instance.Roles)
{
Console.WriteLine(r.ToString());
}
}
}
public enum Role
{
Guest,
Member,
Manager,
Admin
}
}
注意了,一定要添加一个引用:System.Configuration
这里对String进行了一些扩展,使它可以直接当成String对象的方法访问了
顺着asp.net mvc的访问路径,一路到了下来。发现原来还有一个这么简单的方法,这里直接把我简单的DEMO列出来,相信大家都很容易看明白了:
using System;
using System.ComponentModel;
namespace YcoeXu.Common
{
public static class StringExtensions
{
/// <summary>
/// 将字符串格式化成指定的数据类型
/// </summary>
/// <param name="str"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Object Format(this String str, Type type)
{
if (String.IsNullOrEmpty(str))
return null;
if (type == null)
return str;
if (type.IsArray)
{
Type elementType = type.GetElementType();
String[] strs = str.Split(new char[] { ';' });
Array array = Array.CreateInstance(elementType, strs.Length);
for (int i = 0, c = strs.Length; i < c; ++i)
{
array.SetValue(ConvertSimpleType(strs[i], elementType), i);
}
return array;
}
return ConvertSimpleType(str,type);
}
private static object ConvertSimpleType(object value, Type destinationType)
{
object returnValue;
if ((value == null) || destinationType.IsInstanceOfType(value))
{
return value;
}
string str = value as string;
if ((str != null) && (str.Length == 0))
{
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool flag = converter.CanConvertFrom(value.GetType());
if (!flag)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!flag && !converter.CanConvertTo(destinationType))
{
throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);
}
try
{
returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
}
catch (Exception e)
{
throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);
}
return returnValue;
}
}
}
DEMO:
在配置文件里自定义配置:
1. 在<configSections></configSections>节点内添加节点:
<section name="XuConfig" type="System.Configuration.NameValueSectionHandler" />
2. 写配置看起来会是这样的:
<configSections>
//..其它代码
<section name="XuConfig" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<XuConfig>
<add key="ID" value="123"/>
<add key="Name" value="YcoeXu"/>
<add key="Roles" value="Member,Admin"/>
</XuConfig>
写个类自动加载
using System;
using System.Reflection;
using System.Collections.Specialized;
using System.Configuration;
using YcoeXu.Common;
namespace YcoeXu.Test
{
public class XuConfig
{
private XuConfig() { }
private static XuConfig config = null;
private static XuConfig Instance
{
get
{
if (config == null)
{
config = new XuConfig();
Type type = typeof(XuConfig);
//从配置文件里读取XuConfig节点
NameValueCollection xuConfig = (NameValueCollection)ConfigurationManager.GetSection("XuConfig");
//根据Key匹配对应的属性
foreach (String key in xuConfig.AllKeys)
{
PropertyInfo pi = type.GetProperty(key);
if (pi == null || String.IsNullOrEmpty(xuConfig[key]))
continue;
//自动转换类型并注入值
pi.SetValue(config, xuConfig[key].Format(pi.PropertyType), null);
}
}
return config;
}
}
public int ID { set; get; }
public String Name { set; get; }
public Role[] Roles { set; get; }
public void Test()
{
Console.WriteLine(XuConfig.Instance.Name);
Console.WriteLine(XuConfig.Instance.ID);
foreach (Role r in XuConfig.Instance.Roles)
{
Console.WriteLine(r.ToString());
}
}
}
public enum Role
{
Guest,
Member,
Manager,
Admin
}
}
注意了,一定要添加一个引用:System.Configuration
这里对String进行了一些扩展,使它可以直接当成String对象的方法访问了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询