// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; using Microsoft.ClearScript.Util; namespace Microsoft.ClearScript { /// /// Defines extension methods for use with all script engines. /// public static class Extensions { /// /// Converts a type to a host type for use with script code currently running on the /// calling thread. /// /// The type to convert to a host type. /// A host type for use with script code. public static object ToHostType(this Type type) { return type.ToHostType(ScriptEngine.Current); } /// /// Converts a type to a host type for use with script code running in the specified /// script engine. /// /// The type to convert to a host type. /// The script engine in which the host type will be used. /// A host type for use with script code. public static object ToHostType(this Type type, ScriptEngine engine) { MiscHelpers.VerifyNonNullArgument(type, nameof(type)); MiscHelpers.VerifyNonNullArgument(engine, nameof(engine)); return HostItem.Wrap(engine, HostType.Wrap(type)); } /// /// Converts an object to a host object with the specified type restriction, for use with /// script code currently running on the calling thread. /// /// The type whose members are to be made accessible from script code. /// The object to convert to a host object for use with script code. /// A host object with the specified type restriction. public static object ToRestrictedHostObject(this T target) { return target.ToRestrictedHostObject(ScriptEngine.Current); } /// /// Converts an object to a host object with the specified type restriction, for use with /// script code running in the specified script engine. /// /// The type whose members are to be made accessible from script code. /// The object to convert to a host object for use with script code. /// The script engine in which the host object will be used. /// A host object with the specified type restriction. public static object ToRestrictedHostObject(this T target, ScriptEngine engine) { MiscHelpers.VerifyNonNullArgument(target, nameof(target)); MiscHelpers.VerifyNonNullArgument(engine, nameof(engine)); return HostItem.Wrap(engine, target, typeof(T)); } } }