簡體   English   中英

反序列化抽象 Class

[英]Deserialize Abstract Class

我有一個抽象的 class 'Server' 我在我的 UI 中的 JavaScript 中創建,然后我想在我的 Web 服務上有一個方法,它執行以下操作:

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();
}

我遇到的問題是服務器不能被反序列化,因為它是抽象的,我是否需要為我擁有的每個服務器都有一個從具體類型繼承的方法,即

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();
}

我非常感謝您的幫助,因為我不確定什么是最好的解決方案

我收到的確切錯誤是:

無法創建抽象類的實例。

WCF 將支持 inheritance,但您需要使用已知類型屬性來裝飾您的數據合約。 例如:

[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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM