Flo*_*ind 6 c# asp.net-mvc asp.net-mvc-4
我有一个大模型
public class SomeModel
{
public int Id { get; set; }
.....A lot(24) of Fields here.....
}
Run Code Online (Sandbox Code Playgroud)
现在,Post ActionResult Edit(somemodel SomeModel)我想检查用户是否已根据数据库中的原始模型值更改了任何内容.使用If Elsemake会产生很多混乱的代码.无论如何都要检查用户是否更改了某些内容,以及可能的用户更改了哪些字段?
我正在考虑使用这样的方法
public class SomeModel
{
//...
public override bool Equals(object obj)
{
var type = this.GetType();
bool SameObj = true;
//for each public property from 'SomeModel'
//[EDITED]type.GetProperties().Each(prop=>{ // Sorry i'm using custom extension methode here
//you should probably use this instead
type.GetProperties().ToList().ForEach(prop=>{
//dynamically checks that they're equals
if(!prop.GetValue(this,null).Equals(prop.GetValue(obj,null))){
SameObj=false;
}
}
return SameObj;
}
}
Run Code Online (Sandbox Code Playgroud)
/!\ 已编辑
| 归档时间: |
|
| 查看次数: |
925 次 |
| 最近记录: |