Reputation:
I'm trying to cast List<object>
to List<string>
dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem:
List<object> listObject = new List<object>();
listObject.Add("ITEM 1");
listObject.Add("ITEM 2");
listObject.Add("ITEM 3");
List<string> listString = ¿¿listObject??;
Thanks in advance!
Upvotes: 9
Views: 11557
Reputation: 1
My first post... Hope usefull looks work in my project...
public dynamic ConvertList(Type CallingType)
{
dynamic DynamicList;
if (CallingType == TypeOfValue)
{
Type d1 = typeof(List<>);
Type[] typeArgs = { TypeOfValue };
Type DynamicListType = d1.MakeGenericType(typeArgs);
object DynamicListObj = Activator.CreateInstance(DynamicListType);
DynamicList = Convert.ChangeType(DynamicListObj, DynamicListType);
foreach (object ValueElement in ValueRange)
{
dynamic el = Convert.ChangeType(ValueElement, TypeOfValue);
DynamicList.Add(el);
}
}
else //retrun empty List but with right type
{
Type d1 = typeof(List<>);
Type[] typeArgs = { CallingType };
Type DynamicListType = d1.MakeGenericType(typeArgs);
object DynamicListObj = Activator.CreateInstance(DynamicListType);
DynamicList = Convert.ChangeType(DynamicListObj, DynamicListType);
}
return DynamicList;
}
I think that I'll add also a try catch somewhere.
how to test
if (PropertyType == typeof(UInt32))
{
List<UInt32> UInt32_test = NewProperty.ConvertList(PropertyType);
}
if (PropertyType == typeof(string))
{
List<string> string_test = NewProperty.ConvertList(PropertyType);
}
Upvotes: -2
Reputation: 269388
If you can use LINQ then the Cast
method will do what you need:
List<string> listString = listObject.Cast<string>().ToList();
You can also use the ConvertAll
method, as Stan points out in his answer:
List<string> listString = listObject.ConvertAll(x => (string)x);
If you're not using C#3 then you'll need to use the "old" delegate syntax rather than a lambda:
List<string> listString =
listObject.ConvertAll(delegate(object x) { return (string)x; });
Upvotes: 18
Reputation: 16065
If you're using .NET 3.5 you can use, this way you don't have to do an extra ToList(). You can also supply your own converter if you need to convert advanced objects.
List<string> listString = listObject.ConvertAll(x=> x as String);
If you can't use LINQ you can do this
foreach(object item in listObject)
{
string convertedItem = item as String;
if(convertedItem != null)
listString.Add(convertedItem);
}
Upvotes: 4
Reputation: 2279
How bout this:
public static List<T> ConvertToGenericList<T>(IList listOfObjects)
{
List<T> items = new List<T>();
for (int i = 0; i < listOfObjects.Count; i++)
{
items.Add((T)listOfObjects[i]);
}
return items;
}
Usage:
List<object> listObject = new List<object>();
listObject.Add("ITEM 1");
listObject.Add("ITEM 2");
listObject.Add("ITEM 3");
List<string> listString = Converter.ConvertToGenericList<string>(listObject);
Upvotes: 1
Reputation: 12478
List<string> listString = (from o in listObject select (string)o).ToList();
Upvotes: 0
Reputation: 7986
I don't think you can do it one step. Instead, try something like this:
List<object> listObject = new List<object>();
listObject.Add( "ITEM 1" );
listObject.Add( "ITEM 2" );
listObject.Add( "ITEM 3" );
List<string> lstStr = new List<string>( listObject.Count );
foreach ( object obj in listObject )
{
lstStr.Add( obj.ToString() );
}
Upvotes: 0