Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a serializable generic Key/Value pair class in .NET?

Tags:

c#

.net

I'm looking for a key/value pair object that I can include in a web service.

I tried using .NET's System.Collections.Generic.KeyValuePair<> class, but it does not properly serialize in a web service. In a web service, the Key and Value properties are not serialized, making this class useless, unless someone knows a way to fix this.

Is there any other generic class that can be used for this situation?

I'd use .NET's System.Web.UI.Pair class, but it uses Object for its types. It would be nice to use a Generic class, if only for type safety.

like image 806
Dan Herbert Avatar asked Sep 17 '08 13:09

Dan Herbert


People also ask

Is KeyValuePair serializable C#?

KeyValuePair<> class, but it does not properly serialize in a web service. In a web service, the Key and Value properties are not serialized, making this class useless, unless someone knows a way to fix this.

How many types of serialization are there in C#?

Basic and custom serialization Binary and XML serialization can be performed in two ways, basic and custom. Basic serialization uses . NET to automatically serialize the object.

What is serializable class in C#?

Serialization in C# is the process of converting an object into a stream of bytes to store the object to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Can a serializable class have methods?

No. The type information is serialized, along with state. In order to deserialize the data, your program will need to have access to the assemblies containing the types (including methods).


1 Answers

Just define a struct/class.

[Serializable] public struct KeyValuePair<K,V> {   public K Key {get;set;}   public V Value {get;set;} } 
like image 198
leppie Avatar answered Sep 20 '22 17:09

leppie