forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
66 lines (61 loc) · 2.97 KB
/
Extensions.cs
File metadata and controls
66 lines (61 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// Defines extension methods for use with all script engines.
/// </summary>
public static class Extensions
{
/// <summary>
/// Converts a type to a host type for use with script code currently running on the
/// calling thread.
/// </summary>
/// <param name="type">The type to convert to a host type.</param>
/// <returns>A host type for use with script code.</returns>
public static object ToHostType(this Type type)
{
return type.ToHostType(ScriptEngine.Current);
}
/// <summary>
/// Converts a type to a host type for use with script code running in the specified
/// script engine.
/// </summary>
/// <param name="type">The type to convert to a host type.</param>
/// <param name="engine">The script engine in which the host type will be used.</param>
/// <returns>A host type for use with script code.</returns>
public static object ToHostType(this Type type, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(type, "type");
MiscHelpers.VerifyNonNullArgument(engine, "engine");
return HostItem.Wrap(engine, HostType.Wrap(type));
}
/// <summary>
/// Converts an object to a host object with the specified type restriction, for use with
/// script code currently running on the calling thread.
/// </summary>
/// <typeparam name="T">The type whose members are to be made accessible from script code.</typeparam>
/// <param name="target">The object to convert to a host object for use with script code.</param>
/// <returns>A host object with the specified type restriction.</returns>
public static object ToRestrictedHostObject<T>(this T target)
{
return target.ToRestrictedHostObject(ScriptEngine.Current);
}
/// <summary>
/// Converts an object to a host object with the specified type restriction, for use with
/// script code running in the specified script engine.
/// </summary>
/// <typeparam name="T">The type whose members are to be made accessible from script code.</typeparam>
/// <param name="target">The object to convert to a host object for use with script code.</param>
/// <param name="engine">The script engine in which the host object will be used.</param>
/// <returns>A host object with the specified type restriction.</returns>
public static object ToRestrictedHostObject<T>(this T target, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(target, "target");
MiscHelpers.VerifyNonNullArgument(engine, "engine");
return HostItem.Wrap(engine, target, typeof(T));
}
}
}