首页 新闻 会员 周边

有50个字段我需要验证他是否正确 除了 if else 还有更好的办法了吗?

0
悬赏园豆:10 [已解决问题] 解决于 2013-06-20 10:02

有50个字段我需要验证他是否正确  除了 if else 还有更好的办法了吗?

Demon Edge的主页 Demon Edge | 初学一级 | 园豆:36
提问于:2013-06-18 18:01
< >
分享
最佳答案
0
public static T SetModel<T>(this T model, SortedList<string, string> list) 
            where T : Model        {
            PropertyInfo[] propertyInfos = typeof(T).GetProperties();
            foreach (var item in list)
            {
                if (propertyInfos.Count(x => string.Compare(x.Name, item.Key, true) == 0) == 1 && !string.IsNullOrEmpty(item.Value))
                {
                    propertyInfos.First(x => string.Compare(x.Name, item.Key, true) == 0).SetValue(model, item.Value, null);
                }
            }
            return model;
        }
收获园豆:10
淘@淘 | 小虾三级 |园豆:602 | 2013-06-19 09:25
其他回答(3)
0

swicth

网络白蚁 | 园豆:213 (菜鸟二级) | 2013-06-18 18:02
0

你是指JS,还是后台?后台的话,可以用反射+循环搞定。对于js,验证50个字段,就更简单了。

幻天芒 | 园豆:37175 (高人七级) | 2013-06-18 18:16
0

List<T>.Contains();

jerry-Tom | 园豆:4077 (老鸟四级) | 2013-06-19 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册