Domanda

Ho una classe astratta 'Server' che creo nel mio JavaScript nell'interfaccia utente e quindi desidero avere un metodo sul mio servizio Web che esegua le seguenti operazioni:

public List<Database> GetDatabases(Server server)
{
    Type type = server.GetType();
    Server x = null;

    if (typeof (SqlServer2005Server).Equals(type))
    {
        x = new SqlServer2005Server();
    }

    // Return the Databases from the server
    return x.GetDatabases();
}

Il problema che sto riscontrando è che il server non può essere deserializzato in quanto astratto, devo avere un metodo per ogni server che ho che eredita dal tipo concreto, ad esempio

public List<Database> GetDatabases(SqlServer2005Server server)
{
    // Return the Databases from the server
    return SqlServer2005Serverx.GetDatabases();
}

public List<Database> GetDatabases(OracleServer server)
{
    // Return the Databases from the server
    return SqlServer2005Serverx.GetDatabases();
}

Gradirei molto il tuo aiuto in quanto non sono sicuro di quale sia la soluzione migliore

L'errore esatto che ricevo è:

  

Non è possibile creare istanze di classi astratte.

È stato utile?

Soluzione

WCF supporterà l'ereditarietà, ma è necessario decorare il contratto di dati con il tipo noto attibute. Ad esempio:

[DataContract]
[KnownType(typeof(Customer))]
class Contact
{
   [DataMember]
   public string FirstName
   {get;set;}

   [DataMember]
   public string LastName
   {get;set;}
}
[DataContract]
class Customer : Contact
{
   [DataMember]
   public int OrderNumber
   {get;set;}
}

HTH.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top