I have some classes which implement an interface as follows:
class C1 : IpropTemplate { ... }
class C2 : IpropTemplate { ... }
:
Also there is some other class which implements another interface:
class C3 : IclassTemplate { ... }
Now, I need to specify the signature of a property into the IclassTemplate that forces the C3 to have a property which is implemented from IpropTemplate. (such as C1 or C2, etc.)
I tried this:
interface IclassTemplate
{
...
IpropTemplate prop1 { get; set; }
}
class C3 : IclassTemplate
{
...
public C1 prop1
{
get;
set;
}
}
In this case, compiler produces an error indicating that C3 does not implement interface member IclassTemplate.prop1 and that the C3.prop1 cannot implement IpropTemplate.prop1 because it does not have the matching return type of IpropTemplate.
What should I do?
Thanks
You can’t make your C3 implementation only deal in C1 – after all, you’ve guarantee that this will be valid:
If you only need to be able to read the property, it’s slightly easier:
That lets the code in
C3know that it’s really aC1, but still implement the interface.Another option is to make your interface generic:
Hopefully one of these will meet your need – if not, please give more details about what you’re really trying to achieve – the bigger picture.