将每个列表项分配给变量

时间:2013-07-18 09:24:56

标签: c#

我有一个包含许多项目的列表,我想将每个项目分配给一个属性,例如Project1,Project2。

我该怎么做?

感谢您的帮助。

5 个答案:

答案 0 :(得分:2)

您可以尝试以下内容(虽然没有看到相关代码,但很难猜到您实际想要实现的目标):

using System.Reflection;
using System.Collections.Generic;

public class Project { /* ... */ }

public class ProjectSet
{
  public Project Project1 { get; set; }
  public Project Project2 { get; set; }

  /* ... */

  public void AssignProjects(IList<Project> projects)
  {
    Type t = this.GetType();

    for (int i = 0; i < projects.Count; i++)
    {
      PropertyInfo p = t.GetProperty(string.Format("Project{0}", i + 1));

      if (p != null)
        p.SetValue(this, projects[i], null);    
    }
  }
}

答案 1 :(得分:1)

我不确定我明白你的要求......

foreach (var project in projectList)
{
  string propertyName = "Project" + projectList.IndexOf(project).ToString();
  this.GetType().GetProperty(propertyName).SetValue(this, project, null);
}

此代码将每个项目设置为属性Project1,Project2,...

答案 2 :(得分:1)

以下是此问题的一般解决方案:

class Thing
{
    public int Item1 { get; set; }
    public int Item2 { get; set; }
    public int Item3 { get; set; }
    public int Item4 { get; set; }
}

static void Main(string[] args)
{
    var myList = new List<int> {100, 50, 200, 30};
    var objWithProperties = new Thing();
    AssignProperties(objWithProperties, "Item", 1, myList);

}

private static void AssignProperties(object item, string propertyName, int startIndex, IEnumerable values)
{
    int index = startIndex;
    if (item == null) throw new ArgumentNullException("item");
    foreach (var value in values)
    {
        string currentPropertyName = propertyName + index;
        var property = item.GetType().GetProperty(currentPropertyName);
        if (property == null)
        {
            throw new IndexOutOfRangeException("Property " + currentPropertyName + " does not exist on type " + item.GetType());
        }
        property.SetValue(item, value);
        index++;
    }
}

答案 3 :(得分:0)

试试这个:

using System;
using System.Collections.Generic;
using System.Dynamic;

class Program
{
    class Project
    {
        public string Name;
    }
    static void Main(string[] args)
    {
        List<Project> projectList = new List<Project>(){
            new Project(){ Name="0" },
            new Project(){ Name="Project1" },
            new Project(){ Name="Project 1" },
            new Project(){ Name="Project 2" }
        };
        dynamic projectsObject = new ExpandoObject();
        foreach (var project in projectList)
        {
            ((IDictionary<string, object>)projectsObject)
                .Add(project.Name, project);
        }
        // Now you can access the Project1 variable
        Console.WriteLine(projectsObject.Project1.Name);
        // But you need to follow the syntax conventions when 
        // assigning project names, as they will not be available 
        // as properties. Use the following syntax instead:
        Console.WriteLine(
            ((IDictionary<string, dynamic>)projectsObject)["0"].Name);
        Console.WriteLine(
            ((IDictionary<string, dynamic>)projectsObject)["Project 1"].Name);
    }
}

答案 4 :(得分:0)

为此目的,您可能需要Dictionary,例如:

Dictionary<string, ProjectType> projectDict = new Dictionary<string, ProjectType>();
projectDict["Project0"] = ProjectInstance;

如果您可以提供有关您问题的更多信息,您可以获得更好的答案。