Editar

Compartir vía


JavaScriptSerializer.RegisterConverters Method

Definition

Registers a custom converter with the JavaScriptSerializer instance.

public:
 void RegisterConverters(System::Collections::Generic::IEnumerable<System::Web::Script::Serialization::JavaScriptConverter ^> ^ converters);
public void RegisterConverters (System.Collections.Generic.IEnumerable<System.Web.Script.Serialization.JavaScriptConverter> converters);
member this.RegisterConverters : seq<System.Web.Script.Serialization.JavaScriptConverter> -> unit
Public Sub RegisterConverters (converters As IEnumerable(Of JavaScriptConverter))

Parameters

converters
IEnumerable<JavaScriptConverter>

An array that contains the custom converters to be registered.

Exceptions

converters is null.

Examples

The following example shows how to register a custom converter with a JavaScriptSerializer instance. This code example is part of a larger example provided for the JavaScriptSerializer class.

serializer = new JavaScriptSerializer();

// Register the custom converter.
serializer.RegisterConverters(new JavaScriptConverter[] { 
    new System.Web.Script.Serialization.CS.ListItemCollectionConverter() });
    serializer = New JavaScriptSerializer()

    ' Register the custom converter.
    serializer.RegisterConverters(New JavaScriptConverter() _
        {New System.Web.Script.Serialization.VB.ListItemCollectionConverter()})

Remarks

You can register one or more custom converters with a JavaScriptSerializer instance by using the RegisterConverters method. When custom converters are registered, JavaScriptSerializer uses the converters both to serialize managed types and to deserialize JSON strings to managed types.

Multiple converters can be registered with the same JavaScriptSerializer instance, and these converters can indicate support for the same type. As a result, the last converter that is registered for a specific type is the one that will be used by JavaScriptSerializer to serialize the type.

For more information about custom converters, see the JavaScriptConverter class.

Applies to