开发者

How can I populate a class based on an array?

I have an array of data that can have 1 to 10 values in it. Based on the number of values in the array I want to select 1 of 10 differnt classes and place the values of the array in the class object.

This is what I have so far,

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

There are 10 MyObject classes like this,

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.....

How do I loop threw the array to populate the object, s开发者_JAVA百科ince the object is created dynamically?


This class architecture really does look very strange. It appears that you have a convention that class MyObjectX will have exactly X properties named Column1 - ColumnX. I've never seen that before, nor can I think of any scenario where that would be appropriate.

In any case, I would strongly suggest you describe your problem domain and your current architecture so that others might be able to evaluate its appropriateness and perhaps suggest alternatives. For example, it's possible that just need to write one class that encapsulates an array (or maybe some other collection):

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

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

But I will try to answer the question as asked.

You can do something like this:

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

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


What if you had an abstract base class?

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

Then your example becomes:

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.....

Called like so:

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


Try this...

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; }
}

Interface IMyObject identifies your classes. Now create dynamically with Reflection...

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

Hope this will help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜