J
Jon Slaughter
Is it possible to dynamically change the number of elements of one of those
types without actually knowing the element type?
Say I have a
int[] l = new int[30];
But later on in my code I have an object that I know is an array or because
I have
if (s is Array)
{
}
But I do not know what element type it uses... (l could be char's, bytes, or
anything). All I do know is that its an array. (another problem is the
dimensionality)
What I want to do is increase the size of s but since I do not know what
type it is I can't simply use new. I can do something like
if (s is Array)
{
object o = new s.GetType().ElementType()[40];
}
But of course that doesn't work.
I'm sure there is a solution(maybe unsafe code) but haven't been able to
figure it out.
Array does have CreateInstance which seems to do what I want but there then
is the issue with dimensionality.
----------
Heres basically what I'm trying to do(not exactly but is equivalent and
shows the problem).
I have a binary file of numbers. The numbers are arbitrary. I want to supply
the user with a function that they can load the data into arbitrary arrays.
So they might do something like
int[] A;
Load(A);
or
float[] B;
Load(B);
or even
some_class[][][] C;
(its not so arbitrary as the file does have info about the structure of the
arrays and its suppose to correspond to what the user passes but both must
be general enough to handle the different dimensions and element types)
Infact the argument to load is passed as an object because they might just
want to load a single value type or something else. Again, its more
complicated and this example doesn't really show why I'm trying to do what I
do but does show the problem I'm running up against.
Thanks,
Jon
types without actually knowing the element type?
Say I have a
int[] l = new int[30];
But later on in my code I have an object that I know is an array or because
I have
if (s is Array)
{
}
But I do not know what element type it uses... (l could be char's, bytes, or
anything). All I do know is that its an array. (another problem is the
dimensionality)
What I want to do is increase the size of s but since I do not know what
type it is I can't simply use new. I can do something like
if (s is Array)
{
object o = new s.GetType().ElementType()[40];
}
But of course that doesn't work.
I'm sure there is a solution(maybe unsafe code) but haven't been able to
figure it out.
Array does have CreateInstance which seems to do what I want but there then
is the issue with dimensionality.
----------
Heres basically what I'm trying to do(not exactly but is equivalent and
shows the problem).
I have a binary file of numbers. The numbers are arbitrary. I want to supply
the user with a function that they can load the data into arbitrary arrays.
So they might do something like
int[] A;
Load(A);
or
float[] B;
Load(B);
or even
some_class[][][] C;
(its not so arbitrary as the file does have info about the structure of the
arrays and its suppose to correspond to what the user passes but both must
be general enough to handle the different dimensions and element types)
Infact the argument to load is passed as an object because they might just
want to load a single value type or something else. Again, its more
complicated and this example doesn't really show why I'm trying to do what I
do but does show the problem I'm running up against.
Thanks,
Jon