2013-07-18 51 views
-3

我有一個包含多個項目的列表,我想將每個項目分配給一個屬性,例如Project1,Project2。將每個列表項分配給一個變量

我該怎麼做?

感謝您的幫助。

+5

請張貼一些代碼,目前尚不清楚你到底想要做什麼。 – oleksii

+0

在列表中添加屬性,然後添加for循環 – Sandy

+1

「項目」是什麼意思?它是Visual Studio C#項目嗎?列表是什麼意思?它是.NET的'列表'? – astef

回答

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

我不知道我知道你問什麼...

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

此代碼將每個謨設置爲性能PROJECT1,Project2的,...

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); 
    } 
} 
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++; 
    } 
} 
0

您可能需要一個Dictionary爲此目的,例如:

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

如果您可以提供有關你的問題的更多信息,你可能會得到更好的答案。

相關問題