Reputation: 711
I request different SOAP webservices in a C# Programm and handle each result (Webservice1[]
and Webservice2[]
) in different classes but I want to wrap each type of array into the same array or List<T>
of items.
How can i do that, any suggestions?
Regards Rene
Upvotes: 1
Views: 152
Reputation: 6504
Not sure if it's possible or not, but could you try returning something like object[]? This would obviate the need to create a custom base class. Otherwise, perhaps creating a base class for Webservice1 and Webservice2 would allow you to return something like Baseclass[]. Have you looked at WCF, DataContracts, and inheritance? It might allow you a better framework for dealing with these sorts of things, imho.
Upvotes: 0
Reputation: 1062520
It depends a bit on context ;p If they have the exact same API, i.e. they are the same service at two endpoints, then maybe use one service (and change the URL manually), or use WCF which can re-use types between services.
However, a more generic approach would be to exploit partial class
to make both implement a common interface, i.e. if they both have a .Foo
property that you care about, you should be able to do:
namespace SomeNamespace {
public interface ICommon {
int Foo {get;}
}
}
namespace Webservice1Namespace {
partial class Webservice1 : ICommon { }
}
namespace Webservice2Namespace {
partial class Webservice2 : ICommon { }
}
// note we didn't add a .Foo implementation - that comes from the
// *other* half of the partial classes, as per the .designer.cs
This uses implicit interface implementation to map the Foo
from Webservice1
and Webservice2
to ICommon
. Now you can have an ICommon[]
and add instances of either Webservice1
or Webservice2
, and access the .Foo
property of each.
Another approach, of course, is to use dynamic[]
, which would also allow you to access .Foo
, but which throws type-safety out of the window in the process (along with anything that uses reflection, such as data-binding etc).
Upvotes: 3