本文整理汇总了C#中System.Array.CreateInstance方法的典型用法代码示例。如果您正苦于以下问题:C# Array.CreateInstance方法的具体用法?C# Array.CreateInstance怎么用?C# Array.CreateInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.CreateInstance方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a two-dimensional Array of type String.
Array my2DArray=Array.CreateInstance( typeof(String), 2, 3 );
for ( int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++ )
for ( int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++ )
my2DArray.SetValue( "abc" + i + j, i, j );
// Displays the values of the Array.
Console.WriteLine( "The two-dimensional Array contains the following values:" );
PrintValues( my2DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
输出:
The two-dimensional Array contains the following values: abc00 abc01 abc02 abc10 abc11 abc12
示例2: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a one-dimensional Array of type Int32.
Array my1DArray=Array.CreateInstance( typeof(Int32), 5 );
for ( int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
my1DArray.SetValue( i+1, i );
// Displays the values of the Array.
Console.WriteLine( "The one-dimensional Array contains the following values:" );
PrintValues( my1DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
输出:
The one-dimensional Array contains the following values: 1 2 3 4 5
示例3: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a multidimensional Array of type String.
int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
Array my4DArray=Array.CreateInstance( typeof(String), myLengthsArray );
for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ ) {
int[] myIndicesArray = new int[4] { i, j, k, l };
my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
}
// Displays the values of the Array.
Console.WriteLine( "The four-dimensional Array contains the following values:" );
PrintValues( my4DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
输出:
The four-dimensional Array contains the following values: 0000 0001 0002 0003 0004 0010 0011 0012 0013 0014 0020 0021 0022 0023 0024 0030 0031 0032 0033 0034 0100 0101 0102 0103 0104 0110 0111 0112 0113 0114 0120 0121 0122 0123 0124 0130 0131 0132 0133 0134 0200 0201 0202 0203 0204 0210 0211 0212 0213 0214 0220 0221 0222 0223 0224 0230 0231 0232 0233 0234 1000 1001 1002 1003 1004 1010 1011 1012 1013 1014 1020 1021 1022 1023 1024 1030 1031 1032 1033 1034 1100 1101 1102 1103 1104 1110 1111 1112 1113 1114 1120 1121 1122 1123 1124 1130 1131 1132 1133 1134 1200 1201 1202 1203 1204 1210 1211 1212 1213 1214 1220 1221 1222 1223 1224 1230 1231 1232 1233 1234
示例4: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a multidimensional Array of type String.
int[] myLengthsArray = new int[2] { 3, 5 };
int[] myBoundsArray = new int[2] { 2, 3 };
Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
int[] myIndicesArray = new int[2] { i, j };
myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
}
// Displays the lower bounds and the upper bounds of each dimension.
Console.WriteLine( "Bounds:\tLower\tUpper" );
for ( int i = 0; i < myArray.Rank; i++ )
Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintValues( myArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
输出:
Bounds: Lower Upper 0: 2 4 1: 3 7 The Array contains the following values: 23 24 25 26 27 33 34 35 36 37 43 44 45 46 47
示例5: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a three-dimensional Array of type Object.
Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
my3DArray.SetValue( "abc" + i + j + k, i, j, k );
// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
输出:
The three-dimensional Array contains the following values: abc000 abc001 abc002 abc003 abc010 abc011 abc012 abc013 abc020 abc021 abc022 abc023 abc100 abc101 abc102 abc103 abc110 abc111 abc112 abc113 abc120 abc121 abc122 abc123
示例6: Main
//引入命名空间
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;
}