Click or drag to resize

HostFunctionsasTypeT 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.4.5
Syntax
public Object asType<T>(
	Object value
)
where T : class

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
function dispose(value)
{
    var IDisposableT = host.type("System.IDisposable");
    var disposable = host.asType(IDisposableT, value);
    if (disposable) {
        disposable.Dispose();
    }
}
See Also