I'm trying to do a very simple thing - casting a static list of inherited class objects to a list of base class objects. For some reason - in the result, I always get the inherited class objects. I can see that it isn't converting even when debugging inside the lambda expressions. What Am I missing here ?
See my code:
This is the class that contains the static property:
public class InheritedClassRepository
{
public static List<InheritedClass> RepoItems { get; set; }
}
This is the Inherited class:
public class InheritedClass: BaseClass
{
public InheritedClass() { }
public string someProperty { get; set; }
}
This is the base class:
public class BaseClass
{
public BaseClass() { }
public int DeviceId { get; set; }
[JsonProperty("Id")]
public int Id
{
get //For scheme reasons
{
return DeviceId;
}
}
}
These are all the castings and conversions I've tried:
List<BaseClass> a = InheritedClassRepository.RepoItems.Select(item =>
{
BaseClass A = (BaseClass)item;
return A;
}).ToList();
List<BaseClass> b = InheritedClassRepository.RepoItems.Select(item =>
{
BaseClass A = item as BaseClass;
return item as BaseClass;
}).ToList();
List<BaseClass> c = InheritedClassRepository.RepoItems.ConvertAll(item =>
{
BaseClass A = (BaseClass)item;
return (BaseClass)item;
});
List<BaseClass> e = InheritedClassRepository.RepoItems.Cast<BaseClass>().ToList();
List<BaseClass> f = InheritedClassRepository.RepoItems.ConvertAll(item =>
{
BaseClass A = (BaseClass)item;
return (BaseClass)item;
});