Best way to get around not being able to dynamically cast?  
Author Message
xbrady





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

I'm trying to do something where it would be nice to be able to dynamically cast. I know this isn't possible in C# so I'm trying to figure out the best way around this.
Basically the setup is I have a 'Segment' class. Then a have a bunch of classes that inherit from the 'Segment' class.
Then I have a Segment collection class that has an ArrayList that stores different 'Segment's. What I want to be able to do is have a method that takes in a Type (which will be one of the classes that inherits from 'Segment') and have it return an array of Segments of that type.
Something like this:

private ArrayList Segmants;

public Segment[] GetSegmants(Type reqType)
{
     ArrayList thisList = new ArrayList();
     for (int i = 0; i < Segmants.Count; i++)
    {
         if (Segmants[i ].GetType() == reqType)
        {
               thisList.Add(Segmants[i ]);
        }
    }
     return (Segment[])thisList.ToArray();
}

This puts all the objects into thisList that I want but an exception is thrown when I cast it to a Segment[].

Can something like this be done with generics or some other way

Thanks,


Visual C#4  
 
 
Paul Louth





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

public class SegmentManager 
{
    private List<Segment> Segments;

    public List<T> GetSegments<T>() where T : Segment
    {
       List<T> list = new List<T>();

       for (int i = 0; i < Segments.Count; i++)
       {
           if (Segments[ i ].GetType() == typeof(T))
           {
               T item = (T)Segments[ i ];
               list.Add( item );
           }
       }
       return list;
    }
 }

 

So you would call GetSegments<MySegmentSubclass>() to return a List<MySegmentSubclass>.


 
 
xbrady





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

Wow, that is cool! Thank you very much.

 
 
Paul Louth





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

No problem :)
 
 
Paul Louth





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

Sorry, I forgot to change the for loop to a foreach.  foreach is more efficient for dealing with collections/lists/enumerables:

 public class SegmentManager 
{
    private List<Segment> Segments;

    public List<T> GetSegments<T>() where T : Segment
    {
       List<T> list = new List<T>();

       foreach (Segment segment in Segments)
       {
           if (segment.GetType() == typeof(T))
           {
               list.Add( (T)segment );
           }
       }
       return list;
    }
 }

 


 
 
James Curran





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

er..um... you can dynamically cast in C#:



public class SegmentManager
{
private List<Segment> Segments;

public List<T> GetSegments<T>() where T : Segment
{
List<T> list = new List<T>();

foreach (Segment segment in Segments)
{
T t = segment as T;
if (t != null)
{
list.Add(t);
}
}
return list;
}
}



 
 
Paul Louth





PostPosted: Visual C# Language, Best way to get around not being able to dynamically cast? Top

I think dynamic cast was a misnomer in this instance.