2013-07-25 206 views
0

首先,我真的很希望我能够准确地描述我在找什么,所以如果我不清楚,请让我知道,我会尽我所能来澄清。从对象列表中填充类C#

我有两个类,一个是被包装成一个列表的小类。 第二个类使用列表中的值。 这里是类的例子。

class FirstClass 
{ 
    public string value1 {get; set; } 
    public string value2 {get; set; } 
    public string value3 {get; set; } 
    public string value4 {get; set; } 
    public string value5 {get; set; } 
    public string value6 {get; set; } 
} 

class SecondClass 
{ 
    public string FieldName { get; set; } 
    public string FieldValue { get; set; } 
} 


List<SecondClass> sc = new List<SecondClass>(); 

sc[0].FieldName = "value1"; 
sc[0].FieldValue = "Hello World"; 
sc[1].FieldName = "value2"; 
sc[1].FieldValue = "Hello World"; 
sc[2].FieldName = "value3"; 
sc[2].FieldValue = "Hello World"; 
sc[3].FieldName = "value4"; 
sc[3].FieldValue = "Hello World"; 
sc[4].FieldName = "value5"; 
sc[4].FieldValue = "Hello World"; 
sc[5].FieldName = "value6"; 
sc[5].FieldValue = "Hello World"; 

好了,现在你看到了两个类那里,你看二等列表怎么有两个值的每个对象第一个是外地的,在的Firstclass和匹配字段的名称的名称那么你想要应用到FirstClass中的相应字段的值。

那么我该如何将数值从一个传递给另一个? 上帝,我真的希望我清除了,并没有让它太混乱。


编辑编辑编辑


所以我想我有一个更好的方式使用上述信息来解释这个问题。

在指数2

sc[2].FieldName = "value3"; 
sc[2].FieldValue = "Hello World"; 

需求的二等对象被分配到的Firstclass

所以sc[2].FieldName = "value3";FirstClass他们的是一个名为value3一个实际的领域,我们需要分配的 sc[2].FieldValue = "Hello World";的价值FirstClass字段value3

希望能够清除它一点点。

回答

3

我认为你需要使用反射来实现你的目标......即, typeof(FirstClass).GetProperties

在FirstClass的静态构造函数中,我使用Reflection来获取PropertyInfo实例的Dictionary。我这样做一次,并出于性能原因缓存它。然后,当您调用方法SetValueByReflection时,它会查找字典中属性的建议字符串值并设置该值。

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     List<SecondClass> secondClasses = new List<SecondClass>(); 

     secondClasses.Add(new SecondClass { FieldName = "Value1", FieldValue = "Hello World 1" }); 
     secondClasses.Add(new SecondClass { FieldName = "Value2", FieldValue = "Hello World 2" }); 
     secondClasses.Add(new SecondClass { FieldName = "Value3", FieldValue = "Hello World 3" }); 
     secondClasses.Add(new SecondClass { FieldName = "Value4", FieldValue = "Hello World 4" }); 
     secondClasses.Add(new SecondClass { FieldName = "Value5", FieldValue = "Hello World 5" }); 
     secondClasses.Add(new SecondClass { FieldName = "Value6", FieldValue = "Hello World 6" }); 
     // secondClasses.Add(new SecondClass { FieldName = "Value7", FieldValue = "Hello World 7" }); // This line would throw an Exception 

     FirstClass fc = new FirstClass(); 

     foreach (SecondClass secondClass in secondClasses) 
     { 
      fc.SetValueByReflection(secondClass.FieldName, secondClass.FieldValue); 
     } 
    } 
} 

class FirstClass 
{ 
    private static readonly Dictionary<string, PropertyInfo> _publicProperties; 

    static FirstClass() 
    { 
     const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.SetProperty; 

     _publicProperties = typeof(FirstClass).GetProperties(bindingFlags).ToDictionary(propertyInfo => propertyInfo.Name); 
    } 

    public string Value1 { get; set; } 
    public string Value2 { get; set; } 
    public string Value3 { get; set; } 
    public string Value4 { get; set; } 
    public string Value5 { get; set; } 
    public string Value6 { get; set; } 

    /// <summary> 
    /// Sets the value of one of the properties of this class as specified by the propertyName parameter. 
    /// </summary> 
    /// <exception cref="ArgumentException">Thrown when propertyName is not a valid property of FirstClass.</exception> 
    public void SetValueByReflection(string propertyName, string value) 
    { 
     PropertyInfo propertyInfo; 
     _publicProperties.TryGetValue(propertyName, out propertyInfo); 

     if (propertyInfo != null) 
     { 
      propertyInfo.SetValue(this, value, null); 
     } 
     else 
     { 
      throw new ArgumentException("FirstClass does not contain a property of the name " + propertyName, "propertyName"); 
     } 
    } 
} 

class SecondClass 
{ 
    public string FieldName { get; set; } 
    public string FieldValue { get; set; } 
} 
+0

你是我的英雄我以为我将不得不为疯狂的for循环与一个类的开关语句,其中有527个领域。这将是非常疯狂的代码量,你只是简化它在一些相当优雅的代码。非常感谢。 – scripter78

+0

不客气。我在想这个'Ob​​jectDictionary '可能也适合你的需求......但不是100%确定。 http://blogs.msdn.com/b/mitsu/archive/2008/06/18/thinking-about-new-c-method-prototypes-object-as-dictionary.aspx(文章结尾处的代码的zip文件) – Greg

0

如果你必须做很多从一个对象到另一个对象的映射,这是一个很棒的工具。给你一个标准的做法。

AutoMapper

3

这是你在找什么?

FirstClass firstClass = new FirstClass(); // initialize with values. 

List<SecondClass> list = 
    (from prop in FirstClass.GetType().GetProperties() 
    select new SecondClass() 
    { 
     FieldName = prop.Name, 
     FieldValue = prop.GetValue(firstClass, null) 
    }).ToList();