C#将String类型转换成任意基本类型Word格式.docx

上传人:b****4 文档编号:16985579 上传时间:2022-11-27 格式:DOCX 页数:6 大小:16.52KB
下载 相关 举报
C#将String类型转换成任意基本类型Word格式.docx_第1页
第1页 / 共6页
C#将String类型转换成任意基本类型Word格式.docx_第2页
第2页 / 共6页
C#将String类型转换成任意基本类型Word格式.docx_第3页
第3页 / 共6页
C#将String类型转换成任意基本类型Word格式.docx_第4页
第4页 / 共6页
C#将String类型转换成任意基本类型Word格式.docx_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

C#将String类型转换成任意基本类型Word格式.docx

《C#将String类型转换成任意基本类型Word格式.docx》由会员分享,可在线阅读,更多相关《C#将String类型转换成任意基本类型Word格式.docx(6页珍藏版)》请在冰豆网上搜索。

C#将String类型转换成任意基本类型Word格式.docx

  namespaceYcoeXu.Common

  {

  publicstaticclassStringExtensions

  ///<

summary>

  ///将字符串格式化成指定的数据类型

/summary>

paramname="

str"

>

<

/param>

type"

returns>

/returns>

  publicstaticObjectFormat(thisStringstr,Typetype)

  if(String.IsNullOrEmpty(str))

  returnnull;

  if(type==null)

  returnstr;

  if(type.IsArray)

  TypeelementType=type.GetElementType();

  String[]strs=str.Split(newchar[]{'

;

'

});

  Arrayarray=Array.CreateInstance(elementType,strs.Length);

  for(inti=0,c=strs.Length;

i<

c;

++i)

  array.SetValue(ConvertSimpleType(strs[i],elementType),i);

  }

  returnarray;

  returnConvertSimpleType(str,type);

  privatestaticobjectConvertSimpleType(objectvalue,TypedestinationType)

  objectreturnValue;

  if((value==null)||destinationType.IsInstanceOfType(value))

  returnvalue;

欢迎进入.NET社区论坛,与200万技术人员互动交流>

进入

  stringstr=valueasstring;

  if((str!

=null)&

&

(str.Length==0))

  TypeConverterconverter=TypeDescriptor.GetConverter(destinationType);

  boolflag=converter.CanConvertFrom(value.GetType());

  if(!

flag)

  converter=TypeDescriptor.GetConverter(value.GetType());

flag&

!

converter.CanConvertTo(destinationType))

  thrownewInvalidOperationException("

无法转换成类型:

"

+value.ToString()+"

==>

+destinationType);

  try

  returnValue=flag?

converter.ConvertFrom(null,null,value):

converter.ConvertTo(null,null,value,destinationType);

  catch(Exceptione)

类型转换出错:

+destinationType,e);

  returnreturnValue;

  DEMO:

  在配置文件里自定义配置:

  1.在<

configSections>

/configSections>

节点内添加节点:

  <

sectionname="

XuConfig"

type="

System.Configuration.NameValueSectionHandler"

/>

  2.写配置看起来会是这样的:

  //..其它代码

XuConfig>

addkey="

ID"

value="

123"

/>

Name"

YcoeXu"

Roles"

Member,Admin"

/XuConfig>

写个类自动加载

  usingSystem.Reflection;

  usingSystem.Collections.Specialized;

  usingSystem.Configuration;

  usingYcoeXu.Common;

  namespaceYcoeXu.Test

  publicclassXuConfig

  privateXuConfig(){}

  privatestaticXuConfigconfig=null;

  privatestaticXuConfigInstance

  get

  if(config==null)

  config=newXuConfig();

  Typetype=typeof(XuConfig);

  //从配置文件里读取XuConfig节点

  NameValueCollectionxuConfig=(NameValueCollection)ConfigurationManager.GetSection("

);

  //根据Key匹配对应的属性

  foreach(StringkeyinxuConfig.AllKeys)

  PropertyInfopi=type.GetProperty(key);

  if(pi==null||String.IsNullOrEmpty(xuConfig[key]))

  continue;

  //自动转换类型并注入值

  pi.SetValue(config,xuConfig[key].Format(pi.PropertyType),null);

  returnconfig;

  publicintID{set;

get;

}

  publicStringName{set;

  publicRole[]Roles{set;

  publicvoidTest()

  Console.WriteLine(XuConfig.Instance.Name);

  Console.WriteLine(XuConfig.Instance.ID);

  foreach(RolerinXuConfig.Instance.Roles)

  Console.WriteLine(r.ToString());

  publicenumRole

  Guest,

  Member,

  Manager,

  Admin

  注意了,一定要添加一个引用:

System.Configuration

  这里对String进行了一些扩展,使它可以直接当成String对象的方法访问了,是不是很方便呢。

hoho~~~

  项目中的一点点心得,发现网上还没有人放出这种方法,这里就写出来给大家分享下,相信对大家以后进行类似的自动转换或赋值的功能实现会有很大的帮助

  好啦,公司貌似又要转向PHP了,上年刚从java转到C#,明年又要转向其它语言了,hoho~~~

  引用网友的一句话,做人要像柯南这么有霸气,走到哪里,人就死到哪里

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 职高对口

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1