Webservices and Generics

Discussion in 'ASP .Net' started by =?Utf-8?B?TGFycnkgQ2hhcmx0b24=?=, May 22, 2006.

  1. In .Net 2.0, how do I convert between arrays and generics?

    For example in my webservice if I define
    <WebMethod()> _
    Public Function TakeList(ByVal list As List(Of Data)) As List(Of Data)
    Return list
    End Function

    In the application that consumes it all of those lists are arrays.
    ie. the signature of the web service function looks like:
    TakeList(ByRef list As Data())
    Returns Data()

    Now I can see the .Net framework is taking an array (the list parameter from
    me) and morphing it into a generic collection, but how do I do that if I want
    to work with generics in my front-end application?

    Larry
     
    =?Utf-8?B?TGFycnkgQ2hhcmx0b24=?=, May 22, 2006
    #1
    1. Advertising

  2. You have to use Closed type generics in web services. Meaning List<String>
    is ok, but List<T> isn't (where T is unspecified type.) Thems the breaks.
    The bonus though is that your server code won't be boxing things up before
    serialization, but the drawback is that you're in the same boat you were
    before. You have to write a separate web method that accepts or returns each
    specific type of object. If you're working with webservices, and you really
    think about it, this is the ideal behaviour because if you don't specify the
    type, then there isn't a way that the consumer can tell what they're supposed
    to be genererating in terms of a proxy. (WSDL doesn't have a way to declare
    generics, even closed generics like List<string> show up as ArrayOfString in
    WSDL)

    "Larry Charlton" wrote:

    > In .Net 2.0, how do I convert between arrays and generics?
    >
    > For example in my webservice if I define
    > <WebMethod()> _
    > Public Function TakeList(ByVal list As List(Of Data)) As List(Of Data)
    > Return list
    > End Function
    >
    > In the application that consumes it all of those lists are arrays.
    > ie. the signature of the web service function looks like:
    > TakeList(ByRef list As Data())
    > Returns Data()
    >
    > Now I can see the .Net framework is taking an array (the list parameter from
    > me) and morphing it into a generic collection, but how do I do that if I want
    > to work with generics in my front-end application?
    >
    > Larry
     
    =?Utf-8?B?ZGF2ZS5kb2xhbg==?=, May 22, 2006
    #2
    1. Advertising

  3. Thanks, so is the only way to get a List<String> from a String() to write
    some code or is there a built in conversion facility? The web service will
    manage the conversion if in the web service I write:

    Sub(ByRef List(Of String))

    On the client side I must send a String(). Clearly .Net is serializing a
    String() and somehow deserializing List(Of String), what I want to know is
    how? I guess as a fall back I can always code a for each loop and build the
    class I want, but was hoping for built in functionality.
     
    =?Utf-8?B?TGFycnkgQ2hhcmx0b24=?=, May 22, 2006
    #3
  4. You can use the .ToArray method of the generic to convert to a string array,
    but I think you have to call a bunch of Adds to populate the generic list of
    strings with values from a string array. Foreach is nice and simple.

    on your code you can jsut have method something like:

    public List<sting> Listify(string [] strings)
    {
    List<string> toReturn = new List<string>();

    foreach(string s in strings)
    {
    toReturn.Add(s);
    }

    return toReturn;
    }

    Or if you hate making the call yourself,

    You can edit your proxy so that your Set method goes of whatever property
    you're calling it, say "StringList" actually does the above thing by
    manipulating the XmlElelment Attributes of the property

    [XmlElement("StringList")] <-- xml serializer calls this guy for the
    proprety
    public string [] InboundStrings
    {
    get
    {
    return StringList.ToArray(typeof(System.String));
    }
    set
    {
    Listify(value);
    }
    }

    [XmlIgnore]
    public List<string> StringList <----- Your code can use this one.
    {
    get
    {
    ...
    }

    set
    {
    ....
    }
    }


    "Larry Charlton" wrote:

    > Thanks, so is the only way to get a List<String> from a String() to write
    > some code or is there a built in conversion facility? The web service will
    > manage the conversion if in the web service I write:
    >
    > Sub(ByRef List(Of String))
    >
    > On the client side I must send a String(). Clearly .Net is serializing a
    > String() and somehow deserializing List(Of String), what I want to know is
    > how? I guess as a fall back I can always code a for each loop and build the
    > class I want, but was hoping for built in functionality.
    >
    >
     
    =?Utf-8?B?ZGF2ZS5kb2xhbg==?=, May 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,999
    John C. Bollinger
    May 20, 2005
  2. puzzlecracker

    tutorial on generics and threads and io

    puzzlecracker, Jan 29, 2006, in forum: Java
    Replies:
    7
    Views:
    825
    Paulus de Boska
    Jan 31, 2006
  3. Frank Fredstone
    Replies:
    1
    Views:
    407
    =?ISO-8859-1?Q?Tobias_Schr=F6er?=
    Sep 19, 2006
  4. kelvSYC
    Replies:
    2
    Views:
    452
    Piotr Kobzda
    Apr 4, 2007
  5. Soul
    Replies:
    0
    Views:
    524
Loading...

Share This Page