|
1 | | -using Bonsai.Expressions; |
2 | | -using System; |
3 | | -using System.Collections.Generic; |
4 | | -using System.ComponentModel; |
5 | | -using System.Linq; |
6 | | -using System.Linq.Expressions; |
7 | | -using System.Reactive.Linq; |
8 | | -using System.Reflection; |
9 | | - |
10 | | -namespace Bonsai.Scripting |
11 | | -{ |
12 | | - /// <summary> |
13 | | - /// Represents an operator that uses an expression script to invoke an action for |
14 | | - /// each element of an observable sequence. |
15 | | - /// </summary> |
16 | | - [Obsolete] |
17 | | - [DefaultProperty(nameof(Expression))] |
18 | | - [WorkflowElementCategory(ElementCategory.Sink)] |
19 | | - [TypeDescriptionProvider(typeof(ExpressionSinkTypeDescriptionProvider))] |
20 | | - [Description("An expression script used to invoke an action for each element of the sequence.")] |
21 | | - public class ExpressionSink : SingleArgumentExpressionBuilder, IScriptingElement |
22 | | - { |
23 | | - static readonly MethodInfo doMethod = typeof(Observable).GetMethods() |
24 | | - .Single(m => m.Name == nameof(Observable.Do) && |
25 | | - m.GetParameters().Length == 2 && |
26 | | - m.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Action<>)); |
27 | | - |
28 | | - /// <summary> |
29 | | - /// Gets or sets the name of the expression sink. |
30 | | - /// </summary> |
31 | | - [Externalizable(false)] |
32 | | - [Category(nameof(CategoryAttribute.Design))] |
33 | | - [Description("The name of the expression sink.")] |
34 | | - public string Name { get; set; } |
35 | | - |
36 | | - /// <summary> |
37 | | - /// Gets or sets a description for the expression sink. |
38 | | - /// </summary> |
39 | | - [Externalizable(false)] |
40 | | - [Category(nameof(CategoryAttribute.Design))] |
41 | | - [Description("A description for the expression sink.")] |
42 | | - [Editor(DesignTypes.MultilineStringEditor, DesignTypes.UITypeEditor)] |
43 | | - public string Description { get; set; } |
44 | | - |
45 | | - /// <summary> |
46 | | - /// Gets or sets the expression that determines the action to perform |
47 | | - /// on the input elements. |
48 | | - /// </summary> |
49 | | - [Editor("Bonsai.Scripting.Expressions.Design.ExpressionScriptEditor, Bonsai.Scripting.Expressions.Design", DesignTypes.UITypeEditor)] |
50 | | - [Description("The expression that determines the action to perform on the input elements.")] |
51 | | - public string Expression { get; set; } |
52 | | - |
53 | | - /// <inheritdoc/> |
54 | | - public override Expression Build(IEnumerable<Expression> arguments) |
55 | | - { |
56 | | - var expression = Expression; |
57 | | - var source = arguments.First(); |
58 | | - if (!string.IsNullOrEmpty(expression)) |
59 | | - { |
60 | | - var sourceType = source.Type.GetGenericArguments()[0]; |
61 | | - var actionType = System.Linq.Expressions.Expression.GetActionType(sourceType); |
62 | | - var itParameter = new[] { System.Linq.Expressions.Expression.Parameter(sourceType, string.Empty) }; |
63 | | - var onNext = global::System.Linq.Dynamic.DynamicExpression.ParseLambda(actionType, itParameter, null, Expression); |
64 | | - return System.Linq.Expressions.Expression.Call(doMethod.MakeGenericMethod(sourceType), source, onNext); |
65 | | - } |
66 | | - else return source; |
67 | | - } |
68 | | - |
69 | | - class ExpressionSinkTypeDescriptionProvider : TypeDescriptionProvider |
70 | | - { |
71 | | - static readonly TypeDescriptionProvider parentProvider = TypeDescriptor.GetProvider(typeof(ExpressionSink)); |
72 | | - |
73 | | - public ExpressionSinkTypeDescriptionProvider() |
74 | | - : base(parentProvider) |
75 | | - { |
76 | | - } |
77 | | - |
78 | | - public override ICustomTypeDescriptor GetExtendedTypeDescriptor(object instance) |
79 | | - { |
80 | | - return new ScriptingElementTypeDescriptor(instance, |
81 | | - "An expression script used to invoke an action for each element of the sequence."); |
82 | | - } |
83 | | - } |
84 | | - } |
85 | | -} |
| 1 | +using Bonsai.Expressions; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Linq; |
| 6 | +using System.Linq.Expressions; |
| 7 | +using System.Reactive.Linq; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Bonsai.Scripting |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Represents an operator that uses an expression script to invoke an action for |
| 14 | + /// each element of an observable sequence. |
| 15 | + /// </summary> |
| 16 | + [Obsolete] |
| 17 | + [DefaultProperty(nameof(Expression))] |
| 18 | + [WorkflowElementCategory(ElementCategory.Sink)] |
| 19 | + [TypeDescriptionProvider(typeof(ExpressionSinkTypeDescriptionProvider))] |
| 20 | + [Description("An expression script used to invoke an action for each element of the sequence.")] |
| 21 | + public class ExpressionSink : SingleArgumentExpressionBuilder, IScriptingElement |
| 22 | + { |
| 23 | + static readonly MethodInfo doMethod = typeof(Observable).GetMethods() |
| 24 | + .Single(m => m.Name == nameof(Observable.Do) && |
| 25 | + m.GetParameters().Length == 2 && |
| 26 | + m.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Action<>)); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Gets or sets the name of the expression sink. |
| 30 | + /// </summary> |
| 31 | + [Externalizable(false)] |
| 32 | + [Category(nameof(CategoryAttribute.Design))] |
| 33 | + [Description("The name of the expression sink.")] |
| 34 | + public string Name { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets or sets a description for the expression sink. |
| 38 | + /// </summary> |
| 39 | + [Externalizable(false)] |
| 40 | + [Category(nameof(CategoryAttribute.Design))] |
| 41 | + [Description("A description for the expression sink.")] |
| 42 | + [Editor(DesignTypes.MultilineStringEditor, DesignTypes.UITypeEditor)] |
| 43 | + public string Description { get; set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets the expression that determines the action to perform |
| 47 | + /// on the input elements. |
| 48 | + /// </summary> |
| 49 | + [Editor("Bonsai.Scripting.Expressions.Design.ExpressionScriptEditor, Bonsai.Scripting.Expressions.Design", DesignTypes.UITypeEditor)] |
| 50 | + [Description("The expression that determines the action to perform on the input elements.")] |
| 51 | + public string Expression { get; set; } |
| 52 | + |
| 53 | + /// <inheritdoc/> |
| 54 | + public override Expression Build(IEnumerable<Expression> arguments) |
| 55 | + { |
| 56 | + var expression = Expression; |
| 57 | + var source = arguments.First(); |
| 58 | + if (!string.IsNullOrEmpty(expression)) |
| 59 | + { |
| 60 | + var sourceType = source.Type.GetGenericArguments()[0]; |
| 61 | + var actionType = System.Linq.Expressions.Expression.GetActionType(sourceType); |
| 62 | + var itParameter = new[] { System.Linq.Expressions.Expression.Parameter(sourceType, string.Empty) }; |
| 63 | + var onNext = global::System.Linq.Dynamic.DynamicExpression.ParseLambda(actionType, itParameter, null, Expression); |
| 64 | + return System.Linq.Expressions.Expression.Call(doMethod.MakeGenericMethod(sourceType), source, onNext); |
| 65 | + } |
| 66 | + else return source; |
| 67 | + } |
| 68 | + |
| 69 | + class ExpressionSinkTypeDescriptionProvider : TypeDescriptionProvider |
| 70 | + { |
| 71 | + static readonly TypeDescriptionProvider parentProvider = TypeDescriptor.GetProvider(typeof(ExpressionSink)); |
| 72 | + |
| 73 | + public ExpressionSinkTypeDescriptionProvider() |
| 74 | + : base(parentProvider) |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + public override ICustomTypeDescriptor GetExtendedTypeDescriptor(object instance) |
| 79 | + { |
| 80 | + return new ScriptingElementTypeDescriptor(instance, |
| 81 | + "An expression script used to invoke an action for each element of the sequence."); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments