JavaScriptSerializer.ConvertToType<'T> Method (Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Converts the given object to the specified type.

Namespace:   System.Web.Script.Serialization
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

member ConvertToType<'T> : 
        obj:Object -> 'T


Type: System.Object

The object to convert.

Return Value

Type: T

The object that has been converted to the target type.

Type Parameters


The type to which obj will be converted.

Exception Condition

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.


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.


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


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


It is not possible to convert obj to T.


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


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

The ConvertToType<'T> 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<'T> 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<'T> method from the converter code.

The following example shows how to use the ConvertToType<'T> 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.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.5
Return to top