Array.CreateInstance Method : Array « Collections Data Structure « C# / C Sharp






Array.CreateInstance Method

   

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.Demonstrate a one-dimensional arrayDemonstrate a one-dimensional array
2.Declare all kinds of arrays
3.Compute the average of a set of valuesCompute the average of a set of values
4.Enumerates an array using an enumerator object
5.Compute the average of a set of values 2Compute the average of a set of values 2
6.Demonstrate an array overrun
7.Assigning array reference variablesAssigning array reference variables
8.Use the Length array propertyUse the Length array property
9.Reverse an array 2Reverse an array 2
10.illustrates how to use arrays 2illustrates how to use arrays 2
11.illustrates an attempt to write to a nonexistent array elementillustrates an attempt to write to a nonexistent array element
12.illustrates how to initialize arraysillustrates how to initialize arrays
13.illustrates how to use array properties and methodsillustrates how to use array properties and methods
14.illustrates the use of an array of objectsillustrates the use of an array of objects
15.Uses the Array.Copy() method to copy part of an array ints into a secton of an array of doublesUses the Array.Copy() method to copy part of an array ints into a secton of an array of doubles
16.Creates and implements an instance of ArrayCreates and implements an instance of Array
17.Stores a sequence of temperatures in an arrayStores a sequence of temperatures in an array
18.Creates an and array and looks for the index of a given value from either endCreates an and array and looks for the index of a given value from either end
19.Sums the values in an array using a foreach loop 1Sums the values in an array using a foreach loop 1
20.Uses the Array.Copy() method to copy an array of ints into an array of doubles 2Uses the Array.Copy() method to copy an array of ints into an array of doubles 2
21.Class array init
22.Class arrayClass array
23.Multi Dimensional ArraysMulti Dimensional Arrays
24.Jagged Array DemoJagged Array Demo
25.Array reverse and sortArray reverse and sort
26.Use object to create a generic arrayUse object to create a generic array
27.Sort an array and search for a valueSort an array and search for a value
28.Sort and search an array of objectsSort and search an array of objects
29.Reverse an arrayReverse an array
30.Copy an arrayCopy an array
31.Array ConversionsArray Conversions
32.Arrays of Reference Types
33.System.Array Type:ReverseSystem.Array Type:Reverse
34.Array.Sort by CultureInfo
35.A run-time error occurs when Array.Sort is called: XClass does not implement the IComparable interface.
36.Array.SyncRoot Property: synchronize access to an array.
37.Array.Resize Method
38.Array.FindAll Method: This is the syntax of the Predicate delegate:delegate bool Predicate(T obj)
39.Array.Clone Method
40.Array.AsReadOnly Method
41.Dynamic Array