I have a generic interface which I would like to have a property of type self i.e.
public interface IFoo<TBar> : where TBar : ISomething
{
TBar Prop1 { get; set; }
IFoo<TBar> Unset { get; }
}
This is fine until I inherit this and create a (non-generic) class:
public class FooDesired : IFoo<Bar>
{
public Bar Prop1 { get; set; }
public static Foo Unset { get { return new Foo(); } }
}
public class FooReality : IFoo<Bar>
{
public Bar Prop1 { get; set; }
public static IFoo<Bar> Unset { get { return new Foo(); } }
public IFoo<Bar> IFoo<Bar>.Unset { get { return new Foo(); } }
}
I have two issues with the current implementation of this:
1. This implementation does not actual allow Unset to be static. I've worked my way around that with Explicit Interface Implementation but I'm always wary of "tricking" the system.
2. If I call Foo.Unset I always have to cast it back to Foo (unless I setup an implicit operator but that's just hiding the issue rather than solving it).
Edited Real Question: How can I enforce the existence of a static property in a set of classes?
**Edit: ** For those who are keen for a use case, let's assume all Animal species have a set number of bones in their bodies once fully mature. Therefore I would like Animal to enforce a static NumBones property on Cat, Dog and Human. This doesn't cover the static property being of the original class' type but the comments have good links to answers to that.
IFoo<Bar>.Unsetknow whichUnsetmethod to call? And why do you think this would be useful in any case? What's the problem you're trying to solve by adding a static method to an interface?