Edytuj

Udostępnij za pośrednictwem


JavaScriptSerializer.ConvertToType Method

Definition

Overloads

ConvertToType(Object, Type)

Converts the specified object to the specified type.

ConvertToType<T>(Object)

Converts the given object to the specified type.

ConvertToType(Object, Type)

Converts the specified object to the specified type.

public:
 System::Object ^ ConvertToType(System::Object ^ obj, Type ^ targetType);
public object ConvertToType (object obj, Type targetType);
member this.ConvertToType : obj * Type -> obj
Public Function ConvertToType (obj As Object, targetType As Type) As Object

Parameters

obj
Object

The object to convert.

targetType
Type

The type to convert the object to.

Returns

The serialized JSON string.

Exceptions

The resulting JSON-formatted string exceeds the value of MaxJsonLength.

-or-

obj contains a circular reference. A circular reference occurs when a child object has a reference to a parent object, and the parent object has a reference to the child object.

The recursion limit defined by RecursionLimit was exceeded.

Remarks

When the JavaScriptSerializer instance is serializing a type for which a custom converter is registered, the serializer calls the Serialize method to obtain the dictionary of name/value pairs that will be converted to a JSON-formatted string.

The Serialize method can also throw exceptions if the object graph is too complex, or if registered instances of JavaScriptConverter have caused converter recursion.

Applies to

ConvertToType<T>(Object)

Converts the given object to the specified type.

public:
generic <typename T>
 T ConvertToType(System::Object ^ obj);
public T ConvertToType<T> (object obj);
member this.ConvertToType : obj -> 'T
Public Function ConvertToType(Of T) (obj As Object) As T

Type Parameters

T

The type to which obj will be converted.

Parameters

obj
Object

The object to convert.

Returns

T

The object that has been converted to the target type.

Exceptions

obj (or a nested member of obj) contains a "__type" property that indicates a custom type, but the type resolver that is associated with the serializer cannot find a corresponding managed type.

-or-

obj (or a nested member of obj) contains a "__type" property that indicates a custom type, but the result of deserializing the corresponding JSON string cannot be assigned to the expected target type.

-or-

obj (or a nested member of obj) contains a "__type" property that indicates either Object or a non-instantiable type (for example, an abstract type or an interface).

-or-

An attempt was made to convert obj to an array-like managed type, which is not supported for use as a deserialization target.

-or-

It is not possible to convert obj to T.

obj is a dictionary type and a non-string key value was encountered.

-or-

obj includes member definitions that are not available on type T.

Examples

The following example shows how to use the ConvertToType method to obtain a ListItem object from a value in the dictionary that is passed to the converter. This code example is part of a larger example provided for the JavaScriptSerializer class.

ArrayList itemsList = (ArrayList)dictionary["List"];
for (int i=0; i<itemsList.Count; i++)
    list.Add(serializer.ConvertToType<ListItem>(itemsList[i]));
Dim itemsList As ArrayList = CType(dictionary("List"), ArrayList)
Dim i As Integer
For i = 0 To itemsList.Count - 1
    list.Add(serializer.ConvertToType(Of ListItem)(itemsList(i)))
Next i

Remarks

The ConvertToType method tries to convert the object instance that is represented by obj to an instance of type T. During this conversion there is no guarantee that object reference equality is maintained. Therefore, you cannot assume that obj and T refer to the same object.

ConvertToType is intended to be used if you implement a class that derives from JavaScriptConverter. Converter code must be able to take a value that is in the dictionary that the serializer passes to it, and then convert that value to an instance of type T. Instead of re-implementing the custom conversion code to perform this task, you can call the ConvertToType method from the converter code.

Applies to