Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ComInvokeAction.cs85 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.Collections.ObjectModel;6using System.Diagnostics;7using System.Dynamic;8using System.Linq.Expressions;9using System.Runtime.CompilerServices;10 11namespace System.Management.Automation.ComInterop12{13    /// <summary>14    /// Invokes the object. If it falls back, just produce an error.15    /// </summary>16    internal sealed class ComInvokeAction : InvokeBinder17    {18        internal ComInvokeAction(CallInfo callInfo)19            : base(callInfo)20        {21        }22 23        public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)24        {25            if (ComBinder.TryBindInvoke(this, target, args, out DynamicMetaObject res))26            {27                return res;28            }29 30            return errorSuggestion ?? new DynamicMetaObject(31                Expression.Throw(32                    Expression.New(33                        typeof(NotSupportedException).GetConstructor(new[] { typeof(string) }),34                        Expression.Constant(ParserStrings.CannotCall)35                    ),36                    typeof(object)37                ),38                target.Restrictions.Merge(BindingRestrictions.Combine(args))39            );40        }41    }42 43    /// <summary>44    /// Splats the arguments to another nested dynamic site, which does the45    /// real invocation of the IDynamicMetaObjectProvider.46    /// </summary>47    internal sealed class SplatInvokeBinder : CallSiteBinder48    {49        internal static readonly SplatInvokeBinder Instance = new SplatInvokeBinder();50 51        // Just splat the args and dispatch through a nested site52        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)53        {54            Debug.Assert(args.Length == 2);55 56            int count = ((object[])args[1]).Length;57            ParameterExpression array = parameters[1];58 59            var nestedArgs = new ReadOnlyCollectionBuilder<Expression>(count + 1);60            var delegateArgs = new Type[count + 3]; // args + target + returnType + CallSite61            nestedArgs.Add(parameters[0]);62            delegateArgs[0] = typeof(CallSite);63            delegateArgs[1] = typeof(object);64            for (int i = 0; i < count; i++)65            {66                nestedArgs.Add(Expression.ArrayAccess(array, Expression.Constant(i)));67                delegateArgs[i + 2] = typeof(object).MakeByRefType();68            }69            delegateArgs[delegateArgs.Length - 1] = typeof(object);70 71            return Expression.IfThen(72                Expression.Equal(Expression.ArrayLength(array), Expression.Constant(count)),73                Expression.Return(74                    returnLabel,75                    Expression.MakeDynamic(76                        Expression.GetDelegateType(delegateArgs),77                        new ComInvokeAction(new CallInfo(count)),78                        nestedArgs79                    )80                )81            );82        }83    }84}85