Hi guys!
I've got a hypothetical Reseller who can supply some Services; all Services are same: they threat some IRequest data, plus some IExtraData, and provide an IResponse.
Please read this code (working on C# 2.0):
public interface IResellerService<in TIn, in TInExtra, out TOut, in TOutExtra>
where TIn : IRequest
where TInExtra : IExtraData
where TOut : IResponse
where TOutExtra : IExtraData
{
#region Properties
string Code
{
get;
set;
}
//Some other simple properties here...
#endregion
//Some methods there...
}
//Need a collection of IResellerServices; using a IList just to have Add facility
public interface IResellerServices : IList<IResellerService<IRequest, IExtraData, IResponse, IExtraData>>
{
IResellerService<IRequest, IExtraData, IResponse, IExtraData> Get(string code);
void Update(IResellerService<IRequest, IExtraData, IResponse, IExtraData> reseller);
void Delete(string code);
void Disable(string code);
}
public class AvailabilityService : IResellerService<AvailabilityDocumentRequest, AvailabilityInputExtraData, AvailabilityDocumentResponse, AvailabilityOutputExtraData>
{
//Here the interface implementation;
/*
NOTE IResellerService declaration
AvailabilityDocumentRequest : IRequest
AvailabilityInputExtraData : IExtraData
AvailabilityDocumentResponse : IResponse
AvailabilityOutputExtraData : IExtraData
*/
}
[Serializable]
public class Reseller : IReseller
{
#region Properties
public string Code
{
get;
set;
}
[XmlArray("Services")]
[XmlArrayItem("Service")]
public IResellerServices Services
{ get; set; }
//Some other simple properties here...
#endregion
//Some methods there...
}
//Just to make me explain...
void Main()
{
Reseller res = new Reseller;
//Fill reseller properties here...
//...
AvailabilityService service = new AvailabilityService();
//Fill service properties here...
//...
//ERROR!
res.Add(service); //<-- ERROR! Need to cast my AvailabilityService to IResellerService<IRequest, IExtraData, IResponse, IExtraData>
//OK
res.Services.Add(service as IResellerService<IRequest, IExtraData, IResponse, IExtraData>);
}
Well, probably I miss some of base concepts of inheritance or interface implementation, but what I'm doing it wrong?
Hope you can undestand my needs.