For some reason, I am struggling to implement a property from a common interface, using a common base class as follows:
public interface IParent<TChild> where TChild : IChild
{
TChild Child { get; }
}
public interface IChild { }
Then I have a base class:
public class ParentBase<TChild> : IParent<TChild> where TChild : IChild
{
private TChild _child;
public ParentBase(TChild child)
{
this._child = child;
}
#region IParent<TChild> Members
public TChild Child
{
get { return _child; }
}
#endregion
}
Now I have a new parent derivative and child as follows:
public class MyChild : IChild { }
public class MyParent : ParentBase<MyChild>, IParent<IChild>
{
public MyParent(MyChild child)
: base(child)
{
}
}
I want to create an instance and get an abstract (interface type) in order to convey the following to consumers:
IParent<IChild> parent = new MyParent(new MyChild());
But for some reason I cannot implement TChild correctly, although I defined the property public TChild Childin ParentBase, the compiler says that it is not implemented, even if I try to implement it explicitly. Since you can see that the constraints are fully consistent with the base class.
Andre source
share