Hello is there any way to constrain a generic method for classes which are defined inside a static class?
static class Container {
class A {
}
class B {
}
}
static class ContainerExtension
{
//(where T is a class defined in class Container)
public int[] ToArray<T>(T array) {
}
}
Is there any way to constrain a generic method for classes which are defined inside a static class?
No, there isn't.
The only type constraints are:
where T : new()
)where T : class
)where T : struct
)where T : Button
or whatever)There no constraint for "contained within a particular type" nor would I expect that to be a new feature on the way - it sounds like a very niche use case.
You could create an interface that only those types implement, but that's about as close as I can suggest. (If it's a private interface declared in the same containing class, only those classes could implement it - but then the generic method would need to be private too.)
See more on this question at Stackoverflow