Non so come risolvere un problema con interfacce generiche.Ereditarietà da interfacce generiche
Interfaccia generica rappresenta fabbrica per gli oggetti:
interface IFactory<T>
{
// get created object
T Get();
}
interfaccia rappresenta fabbrica per i computer (Classe computer) specyfing fabbrica generale:
interface IComputerFactory<T> : IFactory<T>
where T : Computer
{
// get created computer
new Computer Get();
}
Interfaccia generica rappresenta fabbrica speciale per gli oggetti, che sono clonabile (implementa l'interfaccia System.ICloneable):
interface ISpecialFactory<T>
where T : ICloneable, IFactory<T>
{
// get created object
T Get();
}
Class rappresenta fabbrica per i computer (Classe computer) e oggetti clonabile:
class MyFactory<T> : IComputerFactory<Computer>, ISpecialFactory<T>
{
}
vengono visualizzati messaggi di errore del compilatore in classe myfactory:
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'exer.IFactory<T>'.
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.ICloneable'.
Grazie per la risposta, ma non è esattamente quello che voglio ottenere. Potresti cambiarlo in modo che MyFactory produca tutti gli oggetti clonabili, non solo gli oggetti "Computer"? – Matt
@Matt - Scusa se questo non ha molto senso. Se la fabbrica non si concretizza, come fa a sapere * come * costruire qualcosa? Anche nel tuo post originale 'MyFactory' ha implementato' IComputerFactory'. Dovresti aggiornare la tua domanda con un po 'più di contesto su * cosa stai cercando di ottenere * – Jamiec
Se la fabbrica non si concretizza, come fa a sapere come costruire qualcosa? Pensavo di usare i farmaci generici. Sono i miei post originali MyFactory implementa l'interfaccia IComputerFactory e l'interfaccia ISpecialFactory. Significa che MyFactory è in grado di produrre non solo istanze di classe Computer, ma anche istanze di tutte le classi che sono clonabili. Ho pensato che fosse possibile con il nuovo metodo nella classe myFactory, qualcosa come public T Get {return new T()}. Non so se sia possibile ... – Matt