is it possible to change type of whole array for example, I have:
object[] array;
and I wanna have:
MyObject[] array;
array contains only items of type MyObject but I can see it only as type object[]
this wouldnt be problem if I dont wanna save this array throught reflection to my object which looks like:
class MyArrayObject
{
public MyObject[] objects {get; set;}
}
so if I use reflection – MyArrayObject.GetType().GetProperty("objects").SetValue(array) – I get exception because array if object[] type not MyObject[] – is it possible to change type of array so I can add it through reflection to desired property?
Reason I have object[] not MyObject[] array is, that I wanna add new item into array, so I need to resize it or put to list – and then it changes to object[]
array is not always MyObject[] type – it can be MyObject2, MyObject3 .. anything – I get it through reflection too – so problem is adding item into array whose type is dynamically specified.
Thanks!
That sounds like a reason to use
List<MyObject>to start with. Generally prefer lists over arrays.Sounds like a good place to use generics then. If you really need to use reflection, you can always use
Array.CreateInstancewith the right element type.