J'ai un revendeur hypothétique qui peut fournir certains services; tous les Services sont identiques: ils menacent certaines données IRequest, plus certains IExtraData, et fournissent une réponse IResponse.C# Implémentation de l'interface: faire les bonnes choses
S'il vous plaît lire ce code (travail sur 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>);
}
Eh bien, probablement me manque certains des concepts de base de l'héritage ou la mise en œuvre de l'interface, mais ce que je fais mal?
J'espère que vous pouvez répondre à mes besoins.
Le code ci-dessus * ne fonctionnera pas en C# 2.0 - il utilise la variance générique, qui n'a été introduite qu'en C# 4.0. –
vous devriez lire le concept de variance dans l'interface générique qui est introduite dans .net 4.0 – TalentTuner
show IReseller.Add ou Reseller.Add déclarations – Grozz