I have a generic class on which I define a method that should accept arguments of a different type, but only if another type implements a parameter of the class type. However, this does not compile:
class GenericClass<TClass>
{
public void DoSomething<TMethod>(TMethod input) where TClass : TMethod
{
}
}
I get a compiler error on TClassin method restriction. Is there any way to indicate this?
Explanation:
I get the impression that it TMethod : TClassmeans it TMethodshould inherit or implement TClass(depending on whether it is a TClassparticular type or interface). In another, slightly unconventional notation TMethod > TClass(meaning TMethodis a superset TClass).
What I want here is that it TMethodshould only be a valid type if it TClassinherits or implements it, i.e. TClass > TMethod. Will it TMethod : TClassdo it?
(One answer says it TMethod : TClassrequires that TMethodyou can assign from TClass. I'm not sure if this meets my requirements, but if so, please explain in more detail what can be assigned from means, because if it helps me, I probably misunderstood this ...)
source
share