问题描述:

I have a list containing a number of projects, I want to assign each project to a property, e.g Project1, Project2.

How do I do this?

Thanks for the help.

网友答案:

You might try something like the following (although without seeing the relevant code it's hard to guess what you're actually trying to achieve):

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);    
    }
  }
}
网友答案:

I'm not sure i understand what you ask for ...

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

This code will set each projet to properties Project1, Project2, ...

网友答案:

Here is a generalized solution to this problem:

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++;
    }
}
网友答案:

You might need a Dictionary for this purpose, e.g.:

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

If you can provide more information about your question, you may obtain better answer.

网友答案:

Try this:

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);
    }
}
相关阅读:
Top