4

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?

2 Answers 2

12

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()
Sign up to request clarification or add additional context in comments.

Comments

0

Thats what I would expect but I what I want is: User I need to save the FullName of the type for later, and I wondered if there is a better way, then removing the [] by string replace (or something like that)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.