I have a generic interface, and a few classes implement it.
then from a global place, I want to use methods from that interface,
yet, I don’t know their compiled generic type, so the reference is only their object’s class, as showing in runtime. (so I can’t get access to the interface methods)
a few questions:
- Is it possible to use them?
- should I design it without generics?
- what is the purpose of generic interfaces if I can’t use them at runtime?
- can generics out/in or dynamic help in that situation?
edit: some example code
public interface IMyInterface<T>
where T: class, new()
{
void Delete (T obj);
}
public class trigger {}
public class triggervm : IMyInterface<trigger>
{
List<trigger> _trigList = new List<trigger>()
public void Delete (trigger obj)
{
_trigList.Remove (obj);
}
}
now, say I want to check, and then use the method Delete, from a “global” place:
if (chosenItem is IMyInterface<???>)
{
var item = chosenItem as IMyInterface<???>;
item.Delete(someObj);
}
like thomas suggested, I use dynamic,
and like “RedHat” suggested, I have the enclosing class inherit from both my interface (
IClipboard<T>) and a parent interface (IClippable) that is just a “grouping” interface.myinterface inherits from the grouping interface, so any class that implements myinterface also matches the grouping one.
so I can check if the selected item is IClippable.
the cleanest I could get with my current knowledge of the language.
better support in C# would be nice.