Here's another option. I've gotten this to work, but it sure is ugly code. I use a simple object
variable to hold the generic list. I use methods with constrained type parameters to ensure that I'm using the IFilterableType
interface. I also check types in my DisplayList
method to be sure I'm using the correct implementation of IFilterableType
.
If I call this.DisplayList
using FilterB instead of FilterA, I will get an exception. This is the best solution I can come up with.
public partial class Filter : Window
{
public Filter()
{
List<FilterA> listA = new List<FilterA>();
this.SetList<FilterA>(listA);
this.DisplayList<FilterA>();
}
public interface IFilterableType { string Name { get; } }
public class FilterA : IFilterableType { public string Name { get { return "A"; } } }
public class FilterB : IFilterableType { public string Name { get { return "B"; } } }
private object _myList;
private Type _type;
public void SetList<T>(List<T> list) where T : IFilterableType
{
this._myList = list;
this._type = typeof(T);
}
public void DisplayList<T>() where T : IFilterableType
{
if (this._myList is List<T>)
this.DataContext = (List<T>)this._myList;
else
throw new ArgumentException();
}
}