2011-03-17 83 views
1

我有一個數組可以有1到10個值的數組。根據數組中值的數量,我想選擇10個不同類中的1個,並將數組的值放入類對象中。如何根據數組填充類?

這是我到目前爲止,

Array[] ar; 
ar = PopulateTheArray(); 
int cnt = ar.Count(); 
Object ob = Activator.CreateInstance(null, "MyObject" + cnt); 

有10個爲MyObject類這樣的,

public class MyObject1 
{ 
    public string Column1 { get; set; } 
} 
public class MyObject2 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
} 
public class MyObject3 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
    public string Column3 { get; set; } 
} 
and so on..... 

如何循環扔陣列來填充對象,因爲對象是動態創建?

+3

這是一個家庭作業的問題? – 2011-03-17 21:50:55

+3

你想完成什麼,或者這是一項家庭作業? – 2011-03-17 21:51:04

+0

我希望這是作業,我還沒有牢牢掌握動態構建或如何訪問它們。 – Tim 2011-03-17 21:54:02

回答

1

這個類架構確實看起來很奇怪。看起來你有一個約定那個類MyObjectX將具有名爲Column1 - ColumnX的X屬性。我從來沒有見過這種情況,我也不會想到任何適合的情況。

無論如何,我強烈建議你描述你的問題領域和你當前的架構,以便其他人可以評估它的適當性,並可能提出替代方案。例如,它可能只需要編寫一個類,它封裝一個數組(或者一些其他集合):

public class MyObject 
{ 
    public string[] Columns { get; private set;} 

    public MyObject(int numColumns) 
    { 
     Columns = new string[numColumns]; 
    } 
} 

但我會盡量回答的問題是問。

你可以做這樣的事情:

object ob = ... 
object[] ar = ... 

for (int i = 0; i < ar.Length; i++) 
{ 
    ob.GetType().GetProperty("Column" + i).SetValue(ob, ar[i], null); 
} 
0

如果你有一個抽象基類?

public abstract class MyObjectBase 
{ 
    public abstract void Initialize(params object[] args); 
} 

然後你的例子就變成:

public class MyObject1 : MyObjectBase 
{ 
    public string Column1 { get; set; } 
    public override void Initialize(params object[] args) 
    { 
     this.Column1 = args[0]; 
    } 
} 
public class MyObject2 : MyObjectBase 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
    public override void Initialize(params object[] args) 
    { 
     this.Column1 = args[0]; 
     this.Column2 = args[1]; 
    } 
} 
public class MyObject3 : MyObjectBase 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
    public string Column3 { get; set; } 
    public override void Initialize(params object[] args) 
    { 
     this.Column1 = args[0]; 
     this.Column2 = args[1]; 
     this.Column3 = args[2]; 
    } 
} 
and so on..... 

調用,像這樣:

Array[] ar; 
int cnt = ar.Count(); 
MyObjectBase ob = Activator.CreateInstance(null, "MyObject" + cnt); 
ob.Initialize(ar); 
+0

沒有想到這一點,我可能會爲我工作 – Tim 2011-03-17 22:03:29

+0

「參數」可能沒有必要,但它是一個非常通用的例子:P – mikesigs 2011-03-17 22:05:43

0

嘗試......

public interface IMyObject 
{ 

} 

public class MyObject1 : IMyObject 
{ 
    public string Column1 { get; set; } 
} 
public class MyObject2 : IMyObject 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
} 
public class MyObject3 : IMyObject 
{ 
    public string Column1 { get; set; } 
    public string Column2 { get; set; } 
    public string Column3 { get; set; } 
} 

接口IMyObject標識你的類。現在,動態地創建與反思...

var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes(); 
var instanceList = new List<IMyObject>(); 
foreach (Type currentType in assemblyTypes) 
{ 
    if (currentType.GetInterface("IMyObject") == null) 
    continue; 

    Console.WriteLine("Found type: {0}", currentType); 
    // create instance and add to list 
    instanceList.Add(Activator.CreateInstance(currentType) as IMyObject); 
} 

希望這將有助於