(如果重复,我很抱歉,我不确定是否要检查可空或基元或其他)
我正在创建变量类型的对象数组。它可以是 int、string、Point 或 MyCustomClass(虽然可能没有枚举,但它们与 int 对吗?)。
输入:数组元素的类型。
黑框:检查类型NEEDS是否被构建。创建数组,如果需要构造,则创建每个元素(使用默认值,因为此时它们并不重要)。构造函数必须是无参数的(-> 失败函数)但将字符串视为特殊类型。
输出: object(它的运行时类型是 int[] 或 string[] 或 Point[] 等)
我面临的问题是我创建了充满
null 的数组。基元和结构运行良好,我得到
int[] 没有问题,但类导致“
null[]”。
到目前为止我所拥有的(不确定我是否捕获了它们):
public object createArray(Type arrayElementType, int arrayLength)
{
Array a = Array.CreateInstance(arrayElementType, arrayLength);
if (!arrayElementType.IsPrimitive) // correct would be: if (!arrayElementType.IsValueType)
for (int j = 0; j < arrayLength; j++)
a.SetValue(Activator.CreateInstance(arrayElementType), j);
return a;
}
请您参考如下方法:
这里的困难在于创建实例;很容易找出实例是否会在数组分配时创建:只需检查 default(T) 值。但是我们如何手动创建实例呢?如果你的类有五个构造函数怎么办? 在下面的代码中,如果它的类有一个默认构造函数,它是public并且没有参数,我就创建实例。
public static T[] CreateArray<T>(int size) {
if (size < 0)
throw new ArgumentOutOfRangeException("size");
T[] result = new T[size];
// You may put any special cases here, e.g. if you want empty strings instead of nulls
// uncomment the exerp:
//if (typeof(T) == typeof(String)) {
// for (int i = 0; i < result.Length; ++i)
// result[i] = (T) ((Object) "");
//
// return result;
//}
// If default value is null, instances should be created
// (if we manage to find out how to do it)
if (Object.ReferenceEquals(null, default(T))) {
// Do we have a constructor by default (public one and without parameters)?
ConstructorInfo ci = typeof(T).GetConstructor(new Type[] { });
// If do, let's create instances
if (!Object.ReferenceEquals(null, ci))
for (int i = 0; i < result.Length; ++i)
result[i] = (T) (ci.Invoke(new Object[] { }));
}
return result;
}
测试用例:
// int is a structore, its default value is 0, so i = [0, 0, 0, 0, 0]
int[] i = CreateArray<int>(5);
// String has no String() constructor, so s[] = [null, null, null, null, null]
String[] s = CreateArray<String>(5);
// Button has Button() constructor, so b[] contains buttons
Button[] b = CreateArray<Button>(5);
