首页 新闻 会员 周边

c# 反射之属性类型转换

0
悬赏园豆:10 [已关闭问题] 关闭于 2011-05-30 16:02

自定类: public class User{

public string id{set;get;}

public int Age{set;get;}

。。。。。。}

通过XMLReader读取

Type type;//type是一个自定义类型User类型

PropertyInfo prop = type.GetProperty(reader.Value);

prop.SetValue(o, reader.ReadString(), null);

当Users属性的类型是string时,这样的赋值没有任何问题,但是如果是DataTime、Int、自定义类、时就出错啦,需要进行类型转换。

那么这样在 “prop.SetValue(o, reader.Value, null);”里进行强制类型转换那?

TFS的主页 TFS | 初学一级 | 园豆:89
提问于:2011-05-30 11:04
< >
分享
所有回答(1)
0

呃, 试试用 ReadContentAs 方法...

prop.SetValue(sender, reader.ReadContentAs(prop.PropertyType, Nothing), Nothing)

祭天 | 园豆:202 (菜鸟二级) | 2011-05-30 16:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册