2011-04-29 69 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"; 

你可以对你有什么错误或问题更具体?