ClearScript Library
ClearScript Library
ClearScript Library Reference
Microsoft.ClearScript
HostFunctions Class
HostFunctions Methods
asType<T> Method
cast<T> Method
del<T> Method
flags<T> Method
func Method
getElement Method
getProperty Method
isNull Method
isType<T> Method
isTypeObj Method
newArr Method
newObj Method
newVar<T> Method
proc Method
removeElement Method
removeProperty Method
setElement Method
setProperty Method
toByte Method
toChar Method
toDecimal Method
toDouble Method
toInt16 Method
toInt32 Method
toInt64 Method
toSByte Method
toSingle Method
toStaticType Method
toUInt16 Method
toUInt32 Method
toUInt64 Method
tryCatch Method
typeOf Method

# HostFunctionsasType T Method

Casts an object to the specified host type, returning null if the cast fails.
Namespace: Microsoft.ClearScript
Assembly: ClearScript.Core (in ClearScript.Core.dll) Version: 7.5.0
Syntax
C#
VB
C++
F#
Copy

    public Object asType<T>(
    	Object value
    )
    where T : class

    Public Function asType(Of T As Class) (
    	value As Object
    ) As Object

    public:
    generic<typename T>
    where T : ref class
    Object^ asType(
    	Object^ value
    )

    member asType :
            value : Object -> Object  when 'T : not struct

#### Parameters

value Object
  The object to cast to the specified host type.

#### Type Parameters

T
  The host type to which to cast value.

#### Return Value

Object
The result of the cast if successful, null otherwise.
Remarks
This function is similar to C#'s as operator.
Example
The following code defines a function that disposes an object if it implements IDisposable. It assumes that an instance of ExtendedHostFunctions is exposed under the name "host" (see AddHostObject).
JavaScript
Copy

    function dispose(value)
    {
        var IDisposableT = host.type("System.IDisposable");
        var disposable = host.asType(IDisposableT, value);
        if (disposable) {
            disposable.Dispose();
        }
    }

See Also

#### Reference

HostFunctions Class
Microsoft.ClearScript Namespace
type(String, Object)

Copyright © Microsoft Corporation. All rights reserved.

Send comments on this topic to Microsoft