-4

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

我该怎么做呢?

谢谢您的帮助。

4

5 回答 5

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);    
    }
  }
}
于 2013-07-18T09:34:44.883 回答
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++;
    }
}
于 2013-07-18T09:37:13.930 回答
1

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

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

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

于 2013-07-18T09:35:37.000 回答
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);
    }
}
于 2013-07-18T09:36:38.270 回答
0

Dictionary为此,您可能需要一个,例如:

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

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

于 2013-07-18T09:37:28.707 回答