C# : Implementation of two abstract lists within a non-generic class?
Sorry about the stupid title, had no idea how to word this
I'm creating a class that has two lists of the same type. It's used to copy a reference to a object in the first list to the second list.
While both lists will be of the same type (hold the same type of object) it can be different each time this class is initialized.
So I'm guessing I should make the List types as some kind of abstract list. I would like to ensure they will be strongly typed when instanced (but not neccesary if problematic). The problem is inside the method that moves selected items from list1 to list2 abstract list types normally don't have methods for that.
I guess the normal solution would be to make the class generic ( className<T>
thingy ) but I'm not sure I can do that ( at least I don't know how ) because this class inherits a WPF UserControl.
Here is the Code:
public partial class SubsetSelectionLists : UserControl
{
public static DependencyProperty SetCollectionProperty = DependencyProperty.Register("SetCollection", typeof("Need a abstract list type here"), typeof(SubsetSelectionLists));
public static DependencyProperty SubsetCollectionProperty = DependencyProperty.Register("SubsetCollection", typeof("Need a abstract list type here"), typeof(SubsetSelectionLists));
public "Need a abstract list type here" SetCollection
{
get
{
return ("Need a abstract list type here") GetValue(SetCollectionProperty);
}
set
{
SetValue(SetCollectionProperty, value);
}
}
public "Need a abstract list type here" SubsetCollection
{
get
{
return ("Need a abstract list type here")GetValue(SubsetCollectionProperty);
}
set
{
SetValue(SubsetCollectionProperty, value);
}
}
public SubsetSelectionLists()
{
InitializeComponent();
SubsetSelectiongrid.DataContext = this;
}
private void selectionBtnClick(object sender, RoutedEventArgs e)
{
SubsetCollection.AddTheseItems(S开发者_如何学编程ET.SelecctedItems)
}
private void removeBtnClick(object sender, RoutedEventArgs e)
{
SUBSET.RemoveTheseItems(SUBSET.SelectedItem);
}
}
EDIT: Solution
Some answers pointed me towards the optimal solution of creating a Generic Class. However this can be problematic in WPF. You would have to skip the XAML on the top level generic class.
This would mean that you could do the XAML in the type specific child classes which isn't something you would like to do (unless only the code is something you would re-use but the look is variable). You could also have designed the control using code I guess but I'm not sure how effective that would be.
I was pointed towards the IList object which is a abstract list that many others inherit from and I'm gonna use. It's a bit of a hack but as this will not be used in a open library I'm ok with it. Otherwise I would use the Generic Route.
I don't know how well the designers play with user controls that are generic classes. If that is a problem (which I would guess), one way to get away from that is to expose the collections IList
but still use List<T>
that are constructed in runtime to have type safety in the storage (code sample reduced to include only list object creation and properties for exposing them; add code for DependencyProperty
and so on as needed):
public class YourControl : UserControl
{
// this method will set up the internal lists for accepting
// objects of the specified type only
public void SetListType(Type containedType)
{
var listType = typeof(List<>).MakeGenericType(new[] { containedType });
SetCollection = (IList)Activator.CreateInstance(listType);
SubsetCollection = (IList)Activator.CreateInstance(listType);
}
public IList SetCollection { get; private set; }
public IList SubsetCollection { get; private set; }
}
// usage example:
theControl.SetListType(typeof(string));
theControl.SetCollection.Add("some string"); // works ok
theControl.SetCollection.Add(42); // fails, 42 is not a string
The obvious downside is that SetCollection
and SubsetCollection
expose "untyped" object
lists.
Something like this or am i shooting wide of the mark?
public partial class SubsetSelectionLists<T> : UserControl
{
public List<T> SetCollection { get; set; }
public List<T> SubsetCollection { get; set; }
public SubsetSelectionLists()
{
SubsetSelectiongrid.DataContext = this;
}
private void selectionBtnClick(object sender, RoutedEventArgs e)
{
SubsetCollection.AddRange(SetCollection);
}
private void removeBtnClick(object sender, RoutedEventArgs e)
{
SubsetCollection.RemoveAll(x => SetCollection.Contains(x));
}
}
As you say, a generic class is the way to achieve this and you can do it when inheriting from a non-generic parent class, e.g.
partial class GenericClass1<T> : UserControl
{
private List<T> _list;
}
EDIT based on comments below
Define typed classes that fix the generic type in the above class (and can be used by the designer), i.e.
partial class IntClass1 : GenericClass1<int> {}
partial class StringClass1 : GenericClass1<string> {}
....
Of course this only really works if you have a limited/static set of types you want to hold in your lists
精彩评论