1

I have an ArrayList of objects that are of a certain type, and I need to convert this ArrayList in to a types list. Here is my code

Type objType = Type.GetType(myTypeName);

ArrayList myArrayList = new ArrayList();

object myObj0 = Activator.CreateInstance(type);
object myObj1 = Activator.CreateInstance(type);
object myObj2 = Activator.CreateInstance(type);

myArrayList.Add(myObj0);
myArrayList.Add(myObj1);
myArrayList.Add(myObj2);

Array typedArray = myArrayList.ToArray(objType);  // this is typed
object returnValue = typedArray.ToList();  // this is fake, but this is what I am looking for

There is no ToList() available for an Array, this is the behaviour I am looking for

object returnValue = typedArray.ToList();

So basically I have the type name as a string, I can create a Type from the name, and create a collection containing several objects typed, but how do I convert that to a List? I am hydrating a property and when I do a SetValue my property type needs to match.

Thank you very much.

3
  • Do you know the type at compile-time, or is this dynamic? Commented Aug 17, 2011 at 19:50
  • dynamic, I am converting from JSON and I know the type as a string, and I can create the typed object array, but I need the output to be a List<myStrongType> Commented Aug 17, 2011 at 19:55
  • So... you're trying to covert an Array (typed) to a List<T>, is that right? And you have the name of "type T" stored in a string (and a Type object). Commented Aug 17, 2011 at 20:16

5 Answers 5

5

If you're using .NET 4, dynamic typing can help - it can perform type inference so you can call ToList, admittedly not as an extension method:

dynamic typedArray = myArrayList.ToArray(objType);    
object returnValue = Enumerable.ToList(typedArray);

Otherwise, you'll need to use reflection:

object typedArray = myArrayList.ToArray(objType);   
// It really helps that we don't need to work through overloads...
MethodInfo openMethod = typeof(Enumerable).GetMethod("ToList");
MethodInfo genericMethod = openMethod.MakeGenericMethod(objType);
object result = genericMethod.Invoke(null, new object[] { typedArray });
Sign up to request clarification or add additional context in comments.

3 Comments

If I'm not missing something, you can't call ToList<T> directly on ArrayList, because is not generic. You should call Cast<T> or OfType<T> before that...
@digEmAll: I'm not - I'm calling it on typedArray, which is a typed array resulting from the myArrayList.ToArray(objType) call.
dont argue with john skeet =)
2

Create a

List<YourType> list = new List<YourType>;

and then

list.AddRange(yourArray); 

3 Comments

This won't work, since Array doesn't implement IEnumerable<T>.
I don't have <YourType> available at compile time, I am starting with a string that has the type name, and then I resolve the type as a Type
No need for AddRange() anyway, the List<T> constructor accepts an array of the same type.
1

Use an extenstion method: .ToList<myType>()

2 Comments

ToList() is generic on the source sequence generic type, so that call will fail since ArrayList isn't generic at all.
myType in my case is a Type variable, I don't have the actual type. Cheers.
0

Use the generic List<> type instead.

6 Comments

Isn't that what he's asking? How to convert an array to a generic list?
How do I use List<> when I only have the type as a variable? Thanks.
@Jason Lavigne: Why is your type specified in a string?
@BoltClock: He isn't using generics, so my suggestion is that he does.
@Bernard, because I am converting JSON to a list of typed objects, I am able to populate the object just fine using reflection, but now I need a typed list.
|
0

Does it have to be List<T> or would an IEnumerable<T> do?

Taking a little from Linq you could do:

object returnValue = myArrayList.Cast<string>();

Which creates the following object (assuming T = string):

System.Linq.Enumerable+<CastIterator>d__b1`1[System.String]

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.