Hi,
We really need a bit more info to give better answers.
- can you show a code sample of what you are doing now ?
- What kind of list ? (ArrayList, Array, Generic List)
- What are the real types of the objects in the list
- How is the list filled ?
If it is possible I would try to get the objects as the type they really are in a generic list.
Doing a wild guess and assuming you are using something like an arraylist of objects that are of the same type you could use linq to cast the objects back to the original type.
using System;
using System.Collections;
using System.Linq;
namespace ConsoleApplication1
{
public class Program
{
public class Sample
{
public string Name { get; set; }
}
private static void Main(string[] args)
{
ArrayList listOfObjects = CreateSampleList();
var samples = listOfObjects.Cast<Sample>();
foreach (var sample in samples)
{
Console.WriteLine(sample.Name);
}
Console.ReadLine();
}
private static ArrayList CreateSampleList()
{
var listOfObjects = new ArrayList();
for (int i = 0; i < 1000; i++)
{
var sample = new Sample {Name = string.Format("Name-{0}", i)};
listOfObjects.Add(sample);
}
return listOfObjects;
}
}
}
Or if the objects are in an array of Objects
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
public class Program
{
public class Sample
{
public string Name { get; set; }
}
private static void Main(string[] args)
{
Object[] listOfObjects = CreateSampleList();
var samples = listOfObjects.Cast<Sample>();
foreach (var sample in samples)
{
Console.WriteLine(sample.Name);
}
Console.ReadLine();
}
private static Object[] CreateSampleList()
{
var listOfObjects = new List<Object>();
for (int i = 0; i < 1000; i++)
{
var sample = new Sample {Name = string.Format("Name-{0}", i)};
listOfObjects.Add(sample);
}
return listOfObjects.ToArray();
}
}
}
Hope this helps,
Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.
About me
No comments:
Post a Comment