You can't cast to a variable - you have to explicitly state the type. You'll need to check for each class separately:
if(mylist[i] is FirstType)
{
//do something
}
else if (mylist[i] is SecondType)
{
//do something
}
//etc
A possibly better alternative would be to create 4 separate lists using List<T>
and hold each type separately:
var firstList = new List<FirstType>();
var secondList = new List<SecondType>();
//etc
Or, if possible, create a base class if the data is similar enough and use a List<T>
.
public class MyBase
{
public int Id { get; set; }
}
public class FirstType : MyBase
{
}
public class SecondType : MyBase
{
}
var list = new List<MyBase>();
list.Add(new FirstType());
list.Add(new SecondType());
As mentioned in the comments, you can use partial classes to do this as well. They allow you to define a class in 2 or more files. For example, see how the Profile
class you posted is defined as public partial class Profile : ProfileBase
? You can create your own file and define it again, with other interface implementations. For instance:
public interface IMyInterface
{
public int Id { get; set; }
}
public partial class Profile : IMyInterface
{
public int Id { get; set; }
}
This is a good overview on partial classes: http://msdn.microsoft.com/en-us/library/wa80x488.aspx