2011-04-29 66 views
-1

我有一個asmx webservices的問題。我有那些對象繼承:asmx webservice總是返回子類

public class animal 
{ 
    public string id = null; 
    public string name = null; 
} 

public class dog: animals 
{ 
    public string surname = null; 
    public string color = null; 
} 

和web服務

public animal GetAnimal() 
{ 
    animal result = new dog(); 
    return result; 
} 

的問題是,我的web送花兒給人返回狗。有沒有簡單的方法,以便它可以返回動物? (我看見2個解決方案,我不喜歡:

animal result = new animal(); 

animal resultDog = new dog(); 
animal result = new animal(); 
result.id = resultDog.id 
result.color = resultDog.color 

+0

您的代碼使用webservice的樣子是什麼樣的? – asawyer 2011-04-29 12:25:16

回答

3

的問題是,我的web送花兒給人返回狗

它返回狗型,因爲...這就是它返回的內容

public animal GetAnimal() 
{ 
    animal result = **new dog();** 
    return result; 
} 

你消費的代碼應該能夠refernce它作爲動物類型沒有任何問題:

animal a = GetAnimal(); 
a.id="id"; 
a.name="name"; 

你可以對你有什麼錯誤或問題更具體?