foreach (PropertyInfo pi in t.GetProperties(bf))
{
//把控件属性Text的值赋给entity
pi.SetValue(entity, s,null);
}我想把传过来的字符串 赋给entity 的属性 pi,但是entity的属性类型不一样,所以我想把s 动态转型为 pi.PropertyType,
但是我这样写(pi.PropertyType)s ,编译不了的..Net里面转型操作的时候,是不是一定要类型确定了才能转型?这个在.net里面是不是不能实现?如果遇到这种操作,难道只能用现有的类型去判断?
{
//把控件属性Text的值赋给entity
pi.SetValue(entity, s,null);
}我想把传过来的字符串 赋给entity 的属性 pi,但是entity的属性类型不一样,所以我想把s 动态转型为 pi.PropertyType,
但是我这样写(pi.PropertyType)s ,编译不了的..Net里面转型操作的时候,是不是一定要类型确定了才能转型?这个在.net里面是不是不能实现?如果遇到这种操作,难道只能用现有的类型去判断?
解决方案 »
- 求asp.net 使用动网论坛的傻瓜文档
- 地图经纬度计算问题,帮忙看看,100 分帖子
- 在asp.net mvc框架里对gridview进行分页
- "connectionstring 属性尚未初始化"急求
- 急,急,急………一个怎么样用asp:ButtonColumn取到datagrid里字段的值的问题,在线等………………
- 用Access或者SQL2000做后台开发一个小型网站
- 求助关于ASP.NET的问题
- 用Repeater绑定时,有一CheckBox,如何控制其Checked属性!!
- 怎样实现DataGrid超链接列弹出的页面无标题、无状态栏等?
- 简单问题:.NET中如何弹出提示对话框
- 取后面几个字符
- 求一个javascript函数
switch case
手工转换吧.
{
pi.SetValue(entity, Int32.Parse(textinfo.GetValue(controls[pi.Name], null).ToString()), null);
}
if (tmp == typeof(string))
{
pi.SetValue(entity, (string)textinfo.GetValue(controls[pi.Name], null), null);
} 目前我只能用这种办法,但是当出现一个新类型时,我就要手动添加一个,麻烦.
{
if (entitypi.PropertyType == typeof(int))
{
entitypi.SetValue(entity, Int32.Parse(controlpi.GetValue(control, null).ToString()), null);
}
if (entitypi.PropertyType == typeof(string))
{
entitypi.SetValue(entity, (string)controlpi.GetValue(control, null), null);
}
if (entitypi.PropertyType == typeof(float))
{
entitypi.SetValue(entity, Single.Parse(controlpi.GetValue(control, null).ToString()), null);
}
if (entitypi.PropertyType == typeof(DateTime))
{
entitypi.SetValue(entity, DateTime.Parse(controlpi.GetValue(control, null).ToString()), null);
} return entity;
}最后也只能把这个 封装成一个函数了,要改也只改一个地方,最好能作到这样了.