Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

TrackingCollectionChangedEventArgs Generics #664

Open
stefnotch opened this issue Apr 6, 2018 · 2 comments
Open

TrackingCollectionChangedEventArgs Generics #664

stefnotch opened this issue Apr 6, 2018 · 2 comments

Comments

@stefnotch
Copy link

Is there a reason why TrackingCollectionChangedEventArgs isn't a generic class? TrackingHashSet<T> and other classes are generic, so, it seems like it would make sense to make the events generic as well.
As in:

public class TrackingCollectionChangedEventArgs<T> : EventArgs
{
	public TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction action, T item, T oldItem, int index, bool collectionChanged);
	public TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction action, object key, T item, T oldItem, bool collectionChanged);
	public NotifyCollectionChangedAction Action { get; }
	public T Item { get; }
	public T OldItem { get; }
	public object Key { get; }
	public int Index { get; }
	public bool CollectionChanged { get; }
}
@Kryptos-FR
Copy link

Kryptos-FR commented Apr 7, 2018

How would you register a handler for any kind of collection change? If it was generic you could only register to a specific type. So you would need to know in advance which types will be used in collections and have one handler per type, hardly a workable solution.

@stefnotch
Copy link
Author

Here are a bunch of cases where TrackingCollectionChangedEventArgs gets used:

public event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged

^Not an issue, because TrackingCollection is a generic class.
.
private void GameSystemsOnCollectionChanged(object sender, TrackingCollectionChangedEventArgs trackingCollectionChangedEventArgs)

^Not an issue either, because the programmer clearly knows which types he is expecting.
.
public event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged

^Not an issue, since TrackingHashSet is a generic class.
.
private EventHandler<TrackingCollectionChangedEventArgs> itemAdded;

^TrackingDictionary is generic. And this is probably not an issue either.
.
CollectionChanged += bindings_CollectionChanged;

^VirtualButtonConfig : TrackingCollection<VirtualButtonBinding> The type is known
.
private void Children_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)

^The type is also known here
.
protected void LogicalChildrenChanged(object sender, TrackingCollectionChangedEventArgs trackingCollectionChangedEventArgs)

^The type is known
.

^The interface ITrackingCollectionChanged can easily be turned into a generic one
.
.

data.Emitter.OnMeshUpdate(data.Entity, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null, null));

^This might be an issue. However, if I'm not mistaken, all of those cases can be worked around by having some code that ensures backwards compatibility:

class TrackingCollectionChangedEventArgs<T> 
{
	public static implicit operator TrackingCollectionChangedEventArgs(TrackingCollectionChangedEventArgs<T> stuff)
	{
		return (X<object>)stuff;
	}
}

class TrackingCollectionChangedEventArgs : TrackingCollectionChangedEventArgs<object>
{
			
}

Let me know if you find a case that wouldn't work. Or if I made a mistake somewhere.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants