Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值从类存储到属性名称和值列表中/从属性名称和值列表中恢复_C#_Linq_Collections - Fatal编程技术网

C# 将值从类存储到属性名称和值列表中/从属性名称和值列表中恢复

C# 将值从类存储到属性名称和值列表中/从属性名称和值列表中恢复,c#,linq,collections,C#,Linq,Collections,我不知道最好最简单的方法是什么,所以任何建议都很感激 我想获取任何/all/single域实体类上的所有字段,并在调用特定方法时动态添加前缀/删除前缀 例如,我有如下实体: public class Shop { public string TypeOfShop{get;set} public string OwnerName {get;set} public string Address {get;set} } public class Garage { public string

我不知道最好最简单的方法是什么,所以任何建议都很感激

我想获取任何/all/single域实体类上的所有字段,并在调用特定方法时动态添加前缀/删除前缀

例如,我有如下实体:

public class Shop
{
 public string TypeOfShop{get;set}
 public string OwnerName {get;set}
 public string Address {get;set}
}

public class Garage
{
 public string Company {get;set}
 public string Name {get;set}
 public string Address {get;set}
}
等等

我想获得带有前缀的属性列表:

public Class Simple
{
    public class Prop
    {
     public string Name{get;set;}
     public string Value{get;set;}
    } 

    public ICollection list = new List<Prop>();

    //set all prop
    public void GetPropertiesWithPrefix(Garage mygarage, string prefix)
    {
     list.Add(new Prop{Name = prefix + "_Company", Value = mygarage.Company});
     //so on... upto 50 props...
    }

}

//to get this list I can simple call the list property on the Simple class
公共类简单
{
公共类道具
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
} 
public ICollection list=新列表();
//设置所有道具
public void getProperties with prefix(Garage mygarage,字符串前缀)
{
添加(新属性{Name=prefix+“_Company”,Value=mygarage.Company});
//等等…最多50个道具。。。
}
}
//为了得到这个列表,我可以简单地调用simple类的list属性
读取每个字段时,我使用switch语句并设置值

//Note I return a collection of Prop that have new values set within the view,lets say
//this is a result returned from a controller with the existing prop names and new values...

public MyGarage SetValuesForGarage(MyGarage mygarage, string prefix, ICollection<Prop> _props)
{

  foreach (var item in _prop)
  {
   switch(item.Name)
   {
     case prefix + "Company":
     mygarage.Company = item.Value;
     break;
     //so on for each property...

   }

  }

}
//注意:我返回一个Prop集合,该集合在视图中设置了新值,比如
//这是从具有现有道具名称和新值的控制器返回的结果。。。
公共MyGarage设置值组织(MyGarage MyGarage、字符串前缀、ICollection\u道具)
{
foreach(项目中的var项目)
{
开关(项目名称)
{
案例前缀+“公司”:
mygarage.Company=item.Value;
打破
//因此,对于每个属性。。。
}
}
}

是否有更好、更简单或更优雅的方法来使用linq或其他方式实现此目的?

也许以下方法适合您。它获取任何对象,查找其属性并返回一个包含您的道具对象的列表,每个属性对应一个道具对象

public class PropertyReader
{
    public static List<Prop> GetPropertiesWithPrefix(object obj, string prefix)
    {
        if (obj == null)
        {
            return new List<Prop>();
        }

        var allProps = from propInfo
                       in obj.GetType().GetProperties()
                       select new Prop()
                       {
                           Name = prefix + propInfo.Name,
                           Value = propInfo.GetValue(obj, null) as string
                       };
        return allProps.ToList();
    }
}
公共类PropertyReader
{
公共静态列表GetPropertiesWithPrefix(对象对象,字符串前缀)
{
if(obj==null)
{
返回新列表();
}
var allProps=来自propInfo
在obj.GetType().GetProperties()中
选择新道具()
{
Name=前缀+propInfo.Name,
Value=propInfo.GetValue(obj,null)作为字符串
};
返回allProps.ToList();
}
}

您可以将道具存储在字典中,然后拥有:

mygarage.Company = _props[prefix + "_Company"];
mygarage.Address = _props[prefix + "_Address"];
//And so on...
setvaluesforganage
方法中,而不是内部带有
开关的循环

编辑

有关使用
词典的更多信息,请参阅

您可以定义如下内容:

Dictionary<string, string> list = new Dictionary<string, string>();

这将消除您的
Prop
类。

此Prop类从服务层提供给此方法,因此我可以使用它。ToDictionary()此列表但我要记住数据检查+持久化,上面的示例负责设置值,但在此示例后我忘记了需要验证,问题是,即使我使用字典,它的代码行也几乎与foreach循环和switch语句相同。。。简言之,听起来没有更好的方法吗?
list.Add(prefix + "_Company", mygarage.Company);
list.Add(prefix + "_Address", mygarage.Address);
//And so on...