1

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

4
  • 1
    Do they have a same base class/interface? Commented Aug 10, 2011 at 7:31
  • No they have different base class. I just want a list or a array of items to handle the Array in the same function. I can create a wrapper class to create a list of prop/value over reflection? is there a simple way or do i create a own class? Commented Aug 10, 2011 at 7:34
  • Is there a Type like List<prop,value> Commented Aug 10, 2011 at 7:37
  • 1
    @kockiren: Do you mean something like System.Collections.Generic.Dictionary<key, value>? Commented Aug 10, 2011 at 11:18

2 Answers 2

3

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.