Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
BoundDispEvent.cs101 linesDownload Raw Back to ComInterop
1// Licensed to the .NET Foundation under one or more agreements.2// The .NET Foundation licenses this file to you under the MIT license.3 4using System;5using System.Dynamic;6using System.Linq.Expressions;7using System.Management.Automation.InteropServices;8 9namespace System.Management.Automation.ComInterop10{11    internal sealed class BoundDispEvent : DynamicObject12    {13        private readonly object _rcw;14        private readonly Guid _sourceIid;15        private readonly int _dispid;16 17        internal BoundDispEvent(object rcw, Guid sourceIid, int dispid)18        {19            _rcw = rcw;20            _sourceIid = sourceIid;21            _dispid = dispid;22        }23 24        /// <summary>25        /// Provides the implementation of performing AddAssign and SubtractAssign binary operations.26        /// </summary>27        /// <param name="binder">The binder provided by the call site.</param>28        /// <param name="handler">The handler for the operation.</param>29        /// <param name="result">The result of the operation.</param>30        /// <returns>True if the operation is complete, false if the call site should determine behavior.</returns>31        public override bool TryBinaryOperation(BinaryOperationBinder binder, object handler, out object result)32        {33            if (binder.Operation == ExpressionType.AddAssign)34            {35                result = InPlaceAdd(handler);36                return true;37            }38 39            if (binder.Operation == ExpressionType.SubtractAssign)40            {41                result = InPlaceSubtract(handler);42                return true;43            }44 45            result = null;46            return false;47        }48 49        private static void VerifyHandler(object handler)50        {51            if (handler is Delegate && handler.GetType() != typeof(Delegate))52            {53                return; // delegate54            }55 56            if (handler is IDynamicMetaObjectProvider)57            {58                return; // IDMOP59            }60 61            if (handler is DispCallable)62            {63                return;64            }65 66            throw Error.UnsupportedHandlerType();67        }68 69        /// <summary>70        /// Adds a handler to an event.71        /// </summary>72        /// <param name="handler">The handler to be added.</param>73        /// <returns>The original event with handler added.</returns>74        private object InPlaceAdd(object handler)75        {76            Requires.NotNull(handler);77            VerifyHandler(handler);78 79            ComEventsSink comEventSink = ComEventsSink.FromRuntimeCallableWrapper(_rcw, _sourceIid, true);80            comEventSink.AddHandler(_dispid, handler);81            return this;82        }83 84        /// <summary>85        /// Removes handler from the event.86        /// </summary>87        /// <param name="handler">The handler to be removed.</param>88        /// <returns>The original event with handler removed.</returns>89        private object InPlaceSubtract(object handler)90        {91            Requires.NotNull(handler);92            VerifyHandler(handler);93 94            ComEventsSink comEventSink = ComEventsSink.FromRuntimeCallableWrapper(_rcw, _sourceIid, false);95            comEventSink?.RemoveHandler(_dispid, handler);96 97            return this;98        }99    }100}101