C#中:有没有办法来调整使用反射未知类型的数组? [英] C#: Is there a way to resize an array of unknown type using reflection?

查看:140
本文介绍了C#中:有没有办法来调整使用反射未知类型的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户递给我一些类型的数组,说INT []或字符串[]。我可以很容易地查询类型通过GetElementType的要素,我可以找出多久阵列时,它是通过GetRank,对GetLength等传给我。

My users pass me an array of some type, say int[] or 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.

该阵列通过在PARAMS列表,因此可视化code是这样的:

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);
    }

我想这样做是get方法可用,并且做的工作相反的:我要调整传递给我的阵列,在这种无聊的例子长度设置为其他长度(如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).

我在我的设置数组将包含的数据,因为这样做从一组云计算服务器的接收,我们无法知道有多少人会提前做出反应,因此不能preallocate数组有正确的长度。理想情况下,其实我的用户通过在长度为0的数组,我传回长度为n的数组,表示我得到了n个那名查询服务器回复。

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.

我不能Array.Resize(REF T,INT)这样做,因为我不知道在编译时间T。

I can't do this with Array.Resize(ref T, int) because I don't know T at compile time.

有没有办法拉这一关?

推荐答案

在任何情况下,是好奇,我结束了我的切换code使用List

In case anyone is curious, I ended up switching my code to use List

这篇关于C#中:有没有办法来调整使用反射未知类型的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆