You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I came across a funny issue today, where I have an object of unknown type, and it's composed of several interfaces without having a concrete type or connection between them, to be passed as a single type. Let the code do the talking:
foreach(PropertyInfo property in GetProperties()
{
var value = property.GetValue(obj);
if(value is Blah)
{
}
else if (value is INotifyCollectionChanged incc
&& value is IEnumerable<ITrackableObject> trackableCollection)
/*****************************************************/
//1
void ProcessTrackableCollection<TCollection>(TCollection collection)
where TCollection : INotifyCollectionChanged, IEnumerable<ITrackableObject>
{
//perfect now how r u gonna call it without having a concrete type
}
//2
void ProcessTrackableCollection(object collection)
{
//meh
var incc = (INotifyCollectionChanged)collection;
var tCol = (IEnumerable<ITrackableCollection>)collection;
}
//3
void ProcessTrackableCollection(INotifyCollectionChanged collection)
{
// IEnumerable<ITrackableCollection> will really cry
var tCol = (IEnumerable<ITrackableCollection>)collection;
}
//4
void ProcessTrackableCollection(INotifyCollectionChanged incc, IEnumerable<T> collection)
{ /* :P */ }
I hope the situation is clear by the code itself, so my suggestion is to be able to define aliases of composed type constraints and use them wherever:
using TCollection : INotifyCollectionChanged, IEnumerable<ITrackableObject>;
namesapace Hello
{
public class Program
{
static void Main()
{
PropertyInfo property = GetPropertyInfo();
object obj = (TCollection)property.GetValue(GetItem());
Foo(obj);
}
public static Foo(TCollection obj)
{
obj.CollectionChanged += OnCollectionChanged;
_Items = obj.ToArray(); //no cast
}
}
}
I hope I'm clear enough, should I hear rants and I'll expand.
The text was updated successfully, but these errors were encountered:
weitzhandler
changed the title
Type matching with Generic constraints
Discussion/Suggestion: Type matching with Generic constraints
May 24, 2017
weitzhandler
changed the title
Discussion/Suggestion: Type matching with Generic constraints
Discussion/Suggestion: Type matching with generic constraints
May 24, 2017
I believe that this falls under union/intersection types, which is not something that C# or the CLR supports (outside of generic type constraints). See #228, #344 and #399.
In short, you'd probably want something like:
if(valueis(INotifyCollectionChanged&IEnumerable<ITrackableObject>) collection){// collection treated as both interfaces here}
This is assuming that I am interpreting your "discussion" correctly.
@HaloFour lol I know it was gross, looks like you understood me well, tho I was more about the aliasing part than the union part.
Thanks for the references.
Hi,
I came across a funny issue today, where I have an object of unknown type, and it's composed of several interfaces without having a concrete type or connection between them, to be passed as a single type. Let the code do the talking:
I hope the situation is clear by the code itself, so my suggestion is to be able to define aliases of composed type constraints and use them wherever:
I hope I'm clear enough, should I hear rants and I'll expand.
The text was updated successfully, but these errors were encountered: