简体   繁体   English

C#:有没有一种方法可以使用反射来调整未知类型的数组的大小?

[英]C#: Is there a way to resize an array of unknown type using reflection?

My users pass me an array of some type, say int[] or string[]. 我的用户向我传递了某种类型的数组,例如int []或string []。 I can easily query the types of the elements via GetElementType, and I can find out how long the array was when it was passed to me via GetRank, GetLength, etc. 我可以通过GetElementType轻松查询元素的类型,并且可以找到通过GetRank,GetLength等将数组传递给我时的数组长度。

The arrays are passed in a params list, so visualize code like this: 数组在参数列表中传递,因此可视化如下代码:

    public void Resizer(params object[] objs)
    {
        foreach (object o in objs)
            Array.Resize(ref o, 3);
    }

What I would like to do is the converse of the Get methods that are available and that do work: I want to resize the array that was passed to me, setting the length to some other length (like 3 in this silly example). 我想做的是使用可用的有效方法的相反方法:我想调整传递给我的数组的大小,将长度设置为其他长度(例如在这个愚蠢的示例中为3)。

I'm doing this because in my setting the array will contain data received from a set of cloud computing servers and we can't know how many will respond in advance, hence can't preallocate the array to have the right length. 之所以这样做,是因为在我的设置中,阵列将包含从一组云计算服务器接收的数据,我们不知道有多少将预先响应,因此无法预分配阵列以具有正确的长度。 Ideally, in fact, my user passes in an array of length 0, and I pass back an array of length n, signifying that I got n replies from the servers that were queries. 理想情况下,实际上,我的用户传入的是长度为0的数组,而我传回的是长度为n的数组,这表示我从作为查询的服务器获得了n条回复。

I can't do this with Array.Resize(ref T, int) because I don't know T at compile time. 我不能用Array.Resize(ref T,int)做到这一点,因为在编译时我不知道T。

Is there a way to pull this off? 有没有办法做到这一点?

This should work: 这应该工作:

static void Resize(ref Array array, int newSize) {        
    Type elementType = array.GetType().GetElementType();
    Array newArray = Array.CreateInstance(elementType, newSize);
    Array.Copy(array, newArray, Math.Min(array.Length, newArray.Length));
    array = newArray;
}

Why not just create a new array of whichever type you need that is the size that you want? 为什么不创建所需大小的新数组呢? Then populate it from the array you want to resize, setting non existent values to some default. 然后从您要调整大小的数组中填充它,将不存在的值设置为一些默认值。

万一有人好奇,我最终将代码切换为使用List

I agree with the comments that you should be using List(Of T), but if you want to copy your array into a new array of the same type, you could do something like the following. 我同意应使用List(Of T)的评论,但如果要将数组复制到相同类型的新数组中,则可以执行以下操作。

// Your passed in array.
object[] objs = new object[5] {1,2,3,4,5};

// Create an array of the same type.
Array a = Array.CreateInstance(objs[0].GetType(), objs.Length+3);

// Copy in values.
objs.CopyTo(a,0);

I guess I'll just switch to using Lists, but this is a shame; 我想我只是切换到使用列表,但这太可惜了。 the code will be quite a bit messier looking and since my users are basically at the level of first-semester ugrads, each little thing will make their lives less good. 该代码看起来会有些混乱,并且由于我的用户基本上处于第一学期的水平,所以每件事都会使他们的生活变的不好。 But I'm suspecting that you folks don't see a way to do this either. 但是我怀疑你们中的人也看不到这样做的方法。 Oh well.... 那好吧....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM