Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underlying object Type of an empty Array

Tags:

c#

reflection

Hej,

assuming I have a code that looks like this:

List<User> userList = GetUserByName (u => u.Name == name);
DoSomethingWithTheUsers (userList.ToArray ());

Now I want to know the type of the objects in the Array in the method DoSomethingWithTheUsers (object[] myObjects)

Simply done by myObjects.First ().GetType () but what is to be done if the array is empty? Is there a possibility to still get the Type?

like image 385
Bluenuance Avatar asked Dec 18 '22 10:12

Bluenuance


1 Answers

The array type will be an array of User, i.e. User[]. Why not just use Type.GetElementType() on the GetType() of the array? I.e. using your example:

myObjects.GetType().GetElementType()
like image 176
Barry Kelly Avatar answered Dec 24 '22 01:12

Barry Kelly