C# 将String类型转化成任意类型
2016-10-27 13:41
295 查看
#region 将字符串格式化成指定的数据类型
/// <summary>
/// 将字符串格式化成指定的数据类型
/// </summary>
/// <param name="str"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Object FormatString(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;
}
#endregion
/// <summary>
/// 将字符串格式化成指定的数据类型
/// </summary>
/// <param name="str"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Object FormatString(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;
}
#endregion
相关文章推荐
- [C#] 如何将String类型转换成任意基本类型
- C# string类型和Byte[]相互转化
- [原创][C#] 如何将String类型转换成任意基本类型
- 【原创】C#两种任意类型转换支持(示例:String convert to Type)
- [C#] 如何将String类型转换成任意基本类型
- C#中将string类型的json字符串转化成数组
- 理解C#中的string类型_C#教程
- 理解C#中的string类型
- C++/CLI 中字符串类型的转化: 把std::string转化为System::String
- String 类型转化为Byte[]的代码
- 理解C#中的string类型
- c#中各种数据类型的转化
- 关于C#中货币类型和数值类型、字符串类型的转化和在DataGridView中的显示问题:
- 在C#中,string 类型可与SQL SERVER中的int 类型作比较,是怎么回事?
- c#.net 类型转化
- c#.net 类型转化
- c#.net 类型转化
- 理解C#中的string类型
- C++/CLI 中字符串类型的转化: 把System::String转化为std::string
- 理解C#中的string类型