473,471 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Webservices and Generics

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
May 22 '06 #1
3 2002

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

May 22 '06 #2
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.
May 22 '06 #3

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.

May 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
8
by: Komandur Kannan | last post by:
We have a smart device application running on handhelds(Symbol MC9000G). The backend is Oracle and a middle tier web services development done in Vb.net. We use pessimistic Locking due to...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.