简体   繁体   English

根据实体属性列表设置单个实体属性

[英]Setting individual entity properties based on list of entity properties

I have an intermediary object between my Entity Framework entities and a JSON object that I serialize/deserialize in order to import and export to a text file. 我在实体框架实体和JSON对象之间有一个中间对象,该对象进行序列化/反序列化以导入和导出到文本文件。

When I am exporting from the entity framework I use the following code to iterate through the entity types properties...If the property in the entity matches the property from an enum that I have, the property gets saved to the JSON object. 当我从实体框架导出时,我使用以下代码遍历实体类型的属性...如果实体中的属性与我拥有的枚举中的属性匹配,则该属性将保存到JSON对象。 This stops entity specific properties from cluttering up my JSON. 这可以阻止特定于实体的属性弄乱我的JSON。

var blockProperties = typeof(FixedLengthBlock).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in blockProperties)
{

    if (Enum.GetNames(typeof(ModuleSettingEnum)).Contains(property.Name.ToLower()) && property.GetValue((FixedLengthBlock)element, null) != null)
        blockJsonEntity.Properties.Add(property.Name, property.GetValue((FixedLengthBlock)element, null).ToString());
}

While the above code works, I cannot think of a similar way to do the opposite. 虽然上面的代码有效,但我想不出类似的方法来做相反的事情。 When reading back from JSON I have the properties/values in a dictionary. 从JSON回读时,我在字典中有属性/值。 I know that I can run through the properties of the EF Entity and search the dictionary if it contains a key like this: 我知道我可以遍历EF实体的属性并搜索字典,如果它包含这样的键:

var blockProperties = typeof(FixedLengthBlock).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in blockProperties)
{
        if (block.Properties.ContainsKey(property.Name))
   {
     ???????What goes here??????
   }
}

How do I get the matched property into the newly created entity I've made to receive the information. 如何将匹配的属性放入为接收信息而创建的新创建的实体中。 I'd really like to avoid a large switch statement. 我真的很想避免使用大型switch语句。

My Json object looks like this: 我的Json对象如下所示:

public class JsonEntity
{
    public string Name { get; set; }
    public string Type { get; set; }
    public Dictionary<string, string> Properties { get; set; }
    public List<JsonEntity> SubEntities { get; set; } 

    public JsonEntity()
    {
        Properties = new Dictionary<string, string>();

    }
}

Okay, so if we're deserializing into the same type let's try this: 好的,因此,如果我们要反序列化为相同类型,请尝试以下操作:

var bindingFlags = BindingFlags.Public | BindingFlags.Instance;
var blockProperties = typeof(FixedLengthBlock).GetProperties(bindingFlags);
var newObj = Activator.CreateInstance(typeof(FixedLengthBlock))
foreach (var property in blockProperties)
{
    if (block.Properties.ContainsKey(property.Name))
    {
        var propertyInfo = newObj.GetType().GetProperty(property.Name, bindingFlags);
        if (propertyInfo == null) { continue; }
        propertyInfo.SetValue(newObj, block.Properties[property.Name]);
    }
}

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

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