Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating array of unknown type. C#

While using reflection in C#, you're expected to pass an object[] of the parameters that are cast later on, I have a gui that lets the user input the parameters values in. I know what type of input they are expected to input, int, string, float, instance of custom object, etc... In the case of the argument being an array of some type, int[] foo[], its lets the user construct an array of that type, and add/remove elements.

What I don't know is how I can use the information (i know the type of the data is type t.) How can I construct an array t[], so that when its given to invoke, it can convert to that array type.

For example right now if i have a function that requires an array of integers as an argument, i am currently passing an object[] with another object[] inside it that is filled with integers, but you can't just cast object[] to int[] so the invoke fails.

I can not write a switch case as it's not possible to predict all possible types it could be (instances of some other class defined in a loaded dll, for example)

like image 452
Steven VanHorn Avatar asked Mar 29 '11 14:03

Steven VanHorn


People also ask

Can I initialize an array without size in C?

You can declare an array without a size specifier for the leftmost dimension in multiples cases: as a global variable with extern class storage (the array is defined elsewhere), as a function parameter: int main(int argc, char *argv[]) .

Can you declare an array without assigning the size of an array?

Note that as the arrays in Java are dynamically allocated, we do not specify any dimension or size of the array with the declaration. The above declaration tells the compiler that there is an array variable 'myarray' of type int which will be storing the integer type values in it.

Do C arrays need to be freed?

If the array is declared dynamically, we need to free the memory allocated to it using the free() function.

How can a pointer be set to an unknown type?

Yes, you can create a “void pointer” in C, but strictly speaking, what you're really talking about is a pointer to void (i.e., a pointer to an unknown type of data). Simply use void * as the data type. A pointer to void is also sometimes referred to as a generic pointer.


2 Answers

Are you looking for Array.CreateInstance? That's useful if you only know the type dynamically. If you know it statically but within a generic method, you can just use new T[10] or whatever the type parameter name is.

like image 115
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet


This does not answer the question but I felt it might be an advice worth mentioning.

If it is a method which can be called by passing an array of int as well as array of object it seems that you are implementing a type-free behaviour which can be expressed by generics.

I suggest to change it to a generic method using IEnumerable<T>. Of course, IEnumerable<T> cannot be accessed by index (while array can) but not sure this is what you really need there.

In conjunction with MakeGenericType, it can give you the flexbility you need.

like image 34
Aliostad Avatar answered Oct 04 '22 20:10

Aliostad