开发者

inheritance : asmx webservice alway return subclass

开发者 https://www.devze.com 2023-03-01 08:04 出处:网络
I have a problem with an asmx webservices. I have those object public class animal { public string id = null;

I have a problem with an asmx webservices. I have those object

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

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

a开发者_如何学Gond a webservice

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

the problem is that my webservice alway return a dog. Is there an easy way so it can return an animal? (I see 2 solutions that I don't like:

animal result = new animal();

or

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

)


the problem is that my webservice alway return a dog

It returns a dog type because ... thats what it returns

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

Your consuming code should be able to refernce it as an animal type without any problems:

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

Can you be more specific on what error or problem your having?

0

精彩评论

暂无评论...
验证码 换一张
取 消