Array.CreateInstance : Array « System « C# / C Sharp by API






Array.CreateInstance

  

using System;
using System.Reflection;

public class Starter {
    public static void Main(string[] argv) {
        Assembly executing = Assembly.GetExecutingAssembly();
        Type t = executing.GetType(argv[0]);
        Array zArray = Array.CreateInstance(t, argv.Length - 2);
        for (int count = 2; count < argv.Length; ++count) {
            System.Object obj = Activator.CreateInstance(t, new object[] {argv[count]});
            zArray.SetValue(obj, count - 2);
        }
        foreach (object item in zArray) {
            MethodInfo m = t.GetMethod(argv[1]);
            m.Invoke(item, null);
        }
    }
}

public class MyClass {
    public MyClass(string info) {
        m_Info = "MyClass " + info;
    }

    public void ShowInfo() {

        Console.WriteLine(m_Info);
    }

    private string m_Info;
}

public class YClass {
    public YClass(string info) {
        m_Info = "YClass " + info;
    }

    public void ShowInfo() {
        Console.WriteLine(m_Info);
    }

    private string m_Info;
}

public class XClass {
    public XClass(string info) {
        m_Info = "XClass " + info;
    }

    public void ShowInfo() {
        Console.WriteLine(m_Info);
    }

    private string m_Info;
}

   
    
  








Related examples in the same category

1.Array.AsReadOnly
2.Array.BinarySearch
3.Array.Clone
4.Array.ConvertAll
5.Array.Copy
6.Array.CopyTo
7.Array.Count
8.Array.Exists
9.Array.Find
10.Array.FindAll
11.Array.ForEach
12.Array.GetEnumerator()
13.Array.GetLength()
14.Array.GetLowerbound
15.Array.GetUpperBound
16.Array.IndexOf
17.Array.LastIndexOf
18.Array.Length
19.Array.Rank
20.Array.Resize
21.Array.Reverse
22.Array.Reverse(nums, 1, 3)
23.Array.Sort
24.Array.Sort(names, Comparer.DefaultInvariant)
25.Array.SyncRoot