0

Currently I have an object of type IList<object> and I need to convert to another type of IList using Type object.

eg.

Type t = typeof(MyType);
var list = OtherClass.MyFunction(myParam param);

The var list is of type IList<object> and I need it to be of type IList<t>

Currently I have a static function and call for reflection, but this is called within a loop and need to minimize the maximum time spent in my loops.

Anyone know any other way to perform this conversion without using reflection?

With delegate maybe?


If you experience memory issue with imageViews, make sure you are loading the correct size of image into your imageView.

If your iamgeView is 200*200 and your image is 2000*2000, you need to downsample it instead of loading it directly with its Uri.

Check this link http://developer.android.com/training/displaying-bitmaps/manage-memory.html

4

2 に答える 2

5

使用できますEnumerable.Cast<T>()

var myList = list.Cast<MyType>().ToList();
于 2013-04-25T00:07:56.703 に答える
0

コンパイル時に T がわからない場合は、次のことができると思います。

var type = typeof(int);
IList<object> myList = (new object[]{1, 2, 3, 4, 5}).ToList();
var mi = typeof(Enumerable).GetMethod("Cast");
var mRef = mi.MakeGenericMethod(type);
object myIEnumerableOfInt = mRef.Invoke(null, new object[]{myList});
mi = typeof(Enumerable).GetMethod("ToList");
mRef = mi.MakeGenericMethod(type);
IList myIListOfInt = (IList)mRef.Invoke(null, new object[]{myIEnumerableOfInt});

...しかし、ここでこのリフレクションを使用すると、それを参照できなくなるIList<int>ため、コードで推測できるmyIListOfIntのはIList... IMOここでのキャストはまったく役に立たないことです。

于 2013-04-25T00:34:28.053 に答える