I\'ve been working with generics over the last few days and I encountered an issue by trying to pass generic types as parameters in events/delegates.
A very friendly me
ou can make Catalog class generic. That will allow you to use GenericListItemCountChangedEvent T
public class Catalog where T: BaseItem
{
public delegate void GenericListItemCountChangedEvent(object sender, GenericListItemCountChangedEventArgs e) where T : BaseItem;
//This is the point where it does not work, because I specify BaseItem as the type
public event EventHandler> GenericListItemCountChanged;
private void RaiseGenericListItemCountChangedEvent(List List)
{
if (GenericListItemCountChanged != null)
{
GenericListItemCountChanged(this, new GenericListItemCountChangedEventArgs(List));
}
}
public class GenericListItemCountChangedEventArgs : EventArgs where T : BaseItem
{
private List _changedList_propStore;
public List ChangedList
{
get
{
return _changedList_propStore;
}
}
public GenericListItemCountChangedEventArgs(List ChangedList)
{
_changedList_propStore = ChangedList;
}
}
}
public class MainWindow
{
public MainWindow()
{
new Catalog().GenericListItemCountChanged += (sender, e) => GenericListItemCountChanged(sender, e);
}
private void GenericListItemCountChanged(object sender, Catalog.GenericListItemCountChangedEventArgs e) where T : BaseItem
{
//Use Generic EventArgs
}
}