简体   繁体   English

如何输出更改的字段名称和值的列表?

[英]How can I output a list of field names and values that were changed?

I'm still learning C# whilst building an MVC web app. 在构建MVC网络应用时,我仍在学习C#。 Trying to find a way to create a list of values that were changed by a user during an edit operation. 试图找到一种方法来创建由用户在编辑操作期间更改的值的列表。

Here's one way I have that would work: 这是我可以使用的一种方法:

public List<string> SaveVehicleTechnicalInformation(VehicleAssetTechnicalInformationViewModel editmodel)
    {
        // Create a list of fields that have changed
        List<string> changes = new List<string>();

        var record = db.VehicleAssetTechnicalInformations.Find((int)editmodel.RecordID);

        if (editmodel.Make != null && editmodel.Make != record.Make)
        {
            changes.Add(" [Make changed from " + record.Make + " to " + editmodel.Make + "] ");
            record.Make = editmodel.Make;
        }
        if (editmodel.Model != null && editmodel.Model != record.Model)
        {
            changes.Add(" [Model changed from " + record.Model + " to " + editmodel.Model + "] ");
            record.Model = editmodel.Model;
        }

        return changes;

    }

But... As you can tell, I am going to need to write an IF/ELSE statement for every single field in my database. 但是...正如您所知道的,我将需要为数据库中的每个字段编写一个IF / ELSE语句。 There are about 200 fields in there. 那里大约有200个字段。 I'm also worried that it's going to take a long time to work through the list. 我还担心要花很长时间才能完成列表。

Is there some way to go through the list of properties for my object iteratively, comparing them to the database record, changing them if necessary and then outputting a list of what changed. 有什么方法可以迭代遍历我对象的属性列表,将它们与数据库记录进行比较,如果需要的话可以更改它们,然后输出更改内容的列表。

In pseudo code this is what I guess I am after: 用伪代码,这就是我想得到的:

foreach (var field in editmodel)
        {
            if (field != database.field)
            {
                // Update the value
                // Write a string about what changed
                // Add the string to the list of what changed
            }
        }

Because I'm still learning I would appreciate guidance/tips on what subject matter to read about or where I can independently research the answer. 因为我仍在学习,所以我希望能获得有关要阅读哪些主题或可以在何处独立研究答案的指导/提示。 The gaps in my skill are currently stopping me from being able to even research a solution approach. 我目前技能上的不足使我无法研究解决方案。

Thanks in advance. 提前致谢。

You can try to use Reflection for your purposes. 您可以尝试将Reflection用于您的目的。 Something like this 像这样

var fields = editmodel.GetType().GetFields();
foreach (var item in fields)
{
    if (item.GetValue(editmodel) == database.field)
    {
        // Update the value
        // Write a string about what changed
        // Add the string to the list of what changed
    }
 }

I think I have found the hint I was looking for... 我想我已经找到了寻找的提示...

System.Reflection 系统反射

More specifically, the FieldInfo.GetValue() method. 更具体地说,是FieldInfo.GetValue()方法。

I was previously unaware of what System.Reflection was all about, so I'll research this area further to find my solution. 我以前不知道System.Reflection的全部含义,因此我将进一步研究该领域以找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM