JavaScriptConverter Class

http://www.asp.net/ajax/documentation/live/mref/T_System_Web_Script_Serialization_JavaScriptConverter.aspx

JavaScriptConverter Class

Provides an abstract base class for a custom type converter.

Syntax

CSharp

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class JavaScriptConverter

VisualBasic

<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class JavaScriptConverter

ManagedCPlusPlus

[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class JavaScriptConverter abstract

JSharp

/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public abstract class JavaScriptConverter

Members

Icon Member Description
JavaScriptConverterNew() Initializes a new instance of the JavaScriptConverter class.
Deserialize(IDictionary<(Of String, Object>), Type, JavaScriptSerializer) When overridden in a derived class, converts the provided dictionary into an object of the specified type.
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize() Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode() Serves as a hash function for a particular type. (Inherited from Object.)
GetType() Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Serialize(Object, JavaScriptSerializer) When overridden in a derived class, builds a dictionary of name/value pairs.
SupportedTypes When overridden in a derived class, gets a collection of the supported types.
ToString() Returns a String that represents the current Object. (Inherited from Object.)

Remarks

The JavaScriptConverter class enables you to implement serialization and deserialization processes for managed types that are not natively supported by the JavaScriptSerializer class. You can also use JavaScriptConverter when you need more control over the serialization and deserialization process.

The SupportedTypes property indicates the types for which a custom converter provides converter services.

To indicate that a custom converter needs to be used by the JavaScriptSerializer instance, you must register the converter with the instance. If you are using the JavaScriptSerializer class directly, then you should use the RegisterConverters(IEnumerable<(Of JavaScriptConverter>)) method to register the converter. Otherwise, if you are invoking Web methods from ECMAScript (JavaScript) and you want to use the custom converter, you can register it by adding a converters element in the configuration file. For more information about the configuration elements, see Configuring ASP.NET AJAX.

When the JavaScriptSerializer instance is serializing a type for which it has a custom converter registered, the serializer calls the Serialize(Object, JavaScriptSerializer) method. Similarly, when the JavaScriptSerializer instance is deserializing a JavaScript Object Notation (JSON) string and recognizes that a type inside the JSON string has a custom converter associated with it, the serializer calls the Deserialize(IDictionary<(Of String, Object>), Type, JavaScriptSerializer) method.

Notes For Inheritors

When you inherit from JavaScriptConverter, you must override the following members:

JavaScriptSerializer provides the ConvertToType<(Of T>)(Object) method that will 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 into an object of type T. Rather than re-implementing the custom conversion code to accomplish this, you can call the ConvertToType<(Of T>)(Object) method.

Examples

The following example demonstrates how to create a custom converter for the ListItemCollection class.

Permissions

  • AspNetHostingPermission

    for operating in a hosted environment. Demand value: LinkDemand. Associated enumeration: Minimal.

  • AspNetHostingPermission

    for operating in a hosted environment. Demand value: InheritanceDemand. Associated enumeration: Minimal.

Inheritance Hierarchy

Object

  • JavaScriptConverter

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章