Webservices and Generics

G

Guest

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
 
G

Guest

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

Guest

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

Guest

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
{
....
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Webservices 1
generics puzzle 57
Generics Amanuensis? 2
generics depending on generics 0
Generics - type casting 4
Generics and user controls 8
MVC and Webservices 0
generics 8

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top