I'm trying to cast a concrete class with generics to its interfaces and getting an error. I'm not sure what I'm missing. I need to get them into the common type.
Unable to cast object of type 'HierarchalLocationFilterSet' to type 'ImAFilterSet`1[ModelBase]'.
Everything compiles and runs, I just cant seem to do something like below.
ImAFilterSet<IModelBase> c = new HierarchalLocationFilterSet()
I can go half way ok
var thisWorks = c as FilterSetBase<LocationModel>;
var thisAlsoWorks = c as ImAFilterSet<LocationModel>;
//This is a hard fail
var x = ((ImAFilterSet<ModelBase>) c );
var y = ((ImAFilterSet<IModelBase>) c );
Given
public interface ImAFilterSet<C> where C : IModelBase
{
List<C> Children { get; set; }
}
public abstract class FilterSetBase<C> : ImAFilterSet<C> where C : IModelBase
public class HierarchalLocationFilterSet : FilterSetBase<LocationModel>
public class LocationModel : ModelBase
public abstract class ModelBase : IModelBase
Thanks!