Converts the given object to the specified type.
Syntax
CSharp
public T ConvertToType<T> (
Object obj
)
VisualBasic
Public Function ConvertToType(Of T) ( _
obj As Object _
) As T
ManagedCPlusPlus
public:
generic<typename T>
T ConvertToType (
Object^ obj
)
JSharp
J# supports the use of generic APIs, but not the declaration of new ones.
Generic Template Parameters
- T
The type to which obj will be converted.
Parameters
- obj (Object)
The Object to convert.
Return Value
Return Value
The object converted to the target type.
Remarks
The ConvertToType<(Of T>)(Object) method tries to convert the object instance 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 equals T.
ConvertToType<(Of T>)(Object) is intended to be used by implementers of JavaScriptConverter. Converter code needs the ability to take a value that is contained in the dictionary that the serializer passes to it, and then convert that value to an instance of type T. Rather than re-implementing the custom conversion code to accomplish this, you can call the ConvertToType<(Of T>)(Object) method from the converter code.
Examples
The following example demonstrates how to use the ConvertToType<(Of T>)(Object) method to obtain a ListItem object from a value contained in the dictionary passed to the converter. This code example is part of a larger example provided for the JavaScriptSerializer class.
Exceptions
| Exception | Condition |
|---|
| InvalidOperationException | obj (or a nested member of obj) contains a “__type” tag indicating a custom type, but the type resolver that is currently associated with the serializer cannot find a corresponding managed type. -or- obj (or a nested member of obj) contains a “__type” tag indicating a custom type, but the result of deserializing the corresponding JavaScript Object Notation (JSON) string cannot be assigned to the expected target type. -or- obj (or a nested member of obj) contains a “__type” tag indicating either Object or a non-instantiable type (for example, abstract types and interfaces). -or- An attempt is 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. |
| ArgumentException | obj is a dictionary type and a non-string key value is encountered. -or- obj includes member definitions that are not available on type T. |
Assembly: System.Web.Extensions (Module: System.Web.Extensions)