Helper class to assemble information about a parameter list
        public void Dispatch(object pTarget, uint index, UIAutomationParameter[] pParams, uint cParams)
        {
            // Parse the provider and parameter list
            var provider = (ITestProvider) pTarget;
            var paramList = new UiaParameterListHelper(pParams);

            var member = TestSchema.GetInstance().GetMemberByIndex(index);
            if (member != null)
            {
                member.DispatchCallToProvider(provider, paramList);
                return;
            }

            // Dispatch the method/property calls
            if (index == TestSchema.GetInstance().GetBoolValueMethod.Index)
            {
                paramList[0] = provider.BoolValue;
            }
            else if (index == TestSchema.GetInstance().GetDoubleValueMethod.Index)
            {
                paramList[0] = provider.DoubleValue;
            }
            else if (index == TestSchema.GetInstance().GetElementValueMethod.Index)
            {
                paramList[0] = provider.ElementValue;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        public void DispatchCallToProvider(object provider, UiaParameterListHelper paramList)
        {
            if (ProviderMethodInfo == null)
            {
                throw new InvalidOperationException("You need to pass providerMethodInfo if you want to call ISchemaMember.DispatchCallToProvider");
            }
            if (paramList.Count != PatternMethodParamDescriptions.Count)
            {
                var message = string.Format("Provided paramList has unexpected number of elements. Expected {0} but was {1}",
                                            PatternMethodParamDescriptions.Count,
                                            paramList.Count);
                throw new ArgumentException(message, "paramList");
            }

            var providerCallParameters = new object[ProviderMethodInfo.GetParameters().Length];

            // fill in params
            for (int i = 0; i < PatternMethodParamDescriptions.Count; i++)
            {
                var desc = PatternMethodParamDescriptions[i];
                if (UiaTypesHelper.IsInType(desc.UiaType))
                {
                    var providerMethodParamIdx = _providerMethodInfoIndicies[desc.Name];
                    providerCallParameters[providerMethodParamIdx] = paramList[i];
                }
            }

            // call provider
            object result = null;

            try
            {
                result = ProviderMethodInfo.Invoke(provider, providerCallParameters);
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException != null)
                {
                    throw e.InnerException;
                }
                throw;
            }

            // write back out params
            for (int i = 0; i < PatternMethodParamDescriptions.Count; i++)
            {
                var desc = PatternMethodParamDescriptions[i];
                if (desc.Name == UiaTypesHelper.RetParamUnspeakableName)
                {
                    paramList[i] = result;
                    continue;
                }
                if (UiaTypesHelper.IsOutType(desc.UiaType))
                {
                    var providerMethodParamIdx = _providerMethodInfoIndicies[desc.Name];
                    paramList[i] = providerCallParameters[providerMethodParamIdx];
                }
            }
        }
        // Call a pattern instance method with only in-params
        protected void CallMethod(UiaMethodInfoHelper methodInfo, params object[] methodParams)
        {
            // Create and init a parameter list
            var paramList = new UiaParameterListHelper(methodInfo);
            paramList.Initialize(methodParams);

            // Call through
            PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);
        }
        // Call a pattern instance method with only in-params
        protected void CallMethod(UiaMethodInfoHelper methodInfo, params object[] methodParams)
        {
            // Create and init a parameter list
            var paramList = new UiaParameterListHelper(methodInfo);

            paramList.Initialize(methodParams);

            // Call through
            PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);
        }
        // Get a current property value by calling a method, rather than by using GetProperty
        protected object GetCurrentPropertyValueViaMethod(UiaMethodInfoHelper methodInfo)
        {
            // Create and init a parameter list
            var paramList = new UiaParameterListHelper(methodInfo);
            Debug.Assert(paramList.Count == 1);

            // Call through
            PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);

            // Return the out-parameter
            return paramList[0];
        }
 public void DispatchCallToProvider(object provider, UiaParameterListHelper paramList)
 {
     if (_getterFromProvider == null)
     {
         throw new InvalidOperationException("You have to provide getterFromProvider lambda argument to constructor in order to use this method");
     }
     if (paramList.Count != 1)
     {
         throw new ArgumentException("For a property param list should contain only one out param");
     }
     paramList[0] = _getterFromProvider(provider);
 }
        // Get a current property value by calling a method, rather than by using GetProperty
        protected object GetCurrentPropertyValueViaMethod(UiaMethodInfoHelper methodInfo)
        {
            // Create and init a parameter list
            var paramList = new UiaParameterListHelper(methodInfo);

            Debug.Assert(paramList.Count == 1);

            // Call through
            PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);

            // Return the out-parameter
            return(paramList[0]);
        }
        private void CallMethod(IInvocation invocation, UiaMethodInfoHelper methodHelper)
        {
            if (!methodHelper.SupportsDispatch)
            {
                throw new InvalidOperationException("Called method {0} doesn't support automatic metadata-driven dispatch. You have to modify schema in order to use this feature so that corresponding UiaMethodInfoHelper supports dispatch.");
            }
            var paramList = new UiaParameterListHelper(methodHelper);

            // 1. Fill In params to paramList from invocation arguments
            // we're using the fact that In params are always going before out params, so we may just go through 0..(cInParameters-1)
            for (int i = 0; i < methodHelper.Data.cInParameters; i++)
            {
                var desc = methodHelper.PatternMethodParamDescriptions[i];
                var idx  = methodHelper.GetProviderMethodArgumentIndex(desc.Name);
                paramList[i] = invocation.Arguments[idx];
            }

            // 2. Call patternInstance method
            NativeMethods.WrapUiaComCall(() => _patternInstance.CallMethod(methodHelper.Index, paramList.Data, paramList.Count));

            // 3. Fill Out params back to invocation from paramList
            for (int i = (int)methodHelper.Data.cInParameters; i < methodHelper.Data.cInParameters + methodHelper.Data.cOutParameters; i++)
            {
                object value = paramList[i];
                var    desc  = methodHelper.PatternMethodParamDescriptions[i];
                if (desc.Name == UiaTypesHelper.RetParamUnspeakableName)
                {
                    if (invocation.Method.ReturnType == typeof(AutomationElement))
                    {
                        value = AutomationElement.Wrap((IUIAutomationElement)value);
                    }
                    invocation.ReturnValue = value;
                }
                else
                {
                    var idx = methodHelper.GetProviderMethodArgumentIndex(desc.Name);
                    if (invocation.Method.GetParameters()[idx].ParameterType.GetElementType() == typeof(AutomationElement))
                    {
                        value = AutomationElement.Wrap((IUIAutomationElement)value);
                    }
                    invocation.Arguments[idx] = value;
                }
            }
        }
        public void Dispatch(object pTarget, uint index, UIAutomationParameter[] pParams, uint cParams)
        {
            // Parse the provider and parameter list
            var provider = (IColorProvider) pTarget;
            var paramList = new UiaParameterListHelper(pParams);

            // Dispatch the method/property calls
            if (index == ColorSchema.GetInstance().ValueAsColorProperty.Index)
            {
                paramList[0] = provider.ValueAsColor;
            }
            else if (index == ColorSchema.GetInstance().SetValueAsColorMethod.Index)
            {
                provider.SetValueAsColor((int) paramList[0]);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        private void CallMethod(IInvocation invocation, UiaMethodInfoHelper methodHelper)
        {
            if (!methodHelper.SupportsDispatch)
                throw new InvalidOperationException("Called method {0} doesn't support automatic metadata-driven dispatch. You have to modify schema in order to use this feature so that corresponding UiaMethodInfoHelper supports dispatch.");
            var paramList = new UiaParameterListHelper(methodHelper);

            // 1. Fill In params to paramList from invocation arguments
            // we're using the fact that In params are always going before out params, so we may just go through 0..(cInParameters-1)
            for (int i = 0; i < methodHelper.Data.cInParameters; i++)
            {
                var desc = methodHelper.PatternMethodParamDescriptions[i];
                var idx = methodHelper.GetProviderMethodArgumentIndex(desc.Name);
                paramList[i] = invocation.Arguments[idx];
            }

            // 2. Call patternInstance method
            NativeMethods.WrapUiaComCall(() => _patternInstance.CallMethod(methodHelper.Index, paramList.Data, paramList.Count));

            // 3. Fill Out params back to invocation from paramList
            for (int i = (int)methodHelper.Data.cInParameters; i < methodHelper.Data.cInParameters + methodHelper.Data.cOutParameters; i++)
            {
                object value = paramList[i];
                var desc = methodHelper.PatternMethodParamDescriptions[i];
                if (desc.Name == UiaTypesHelper.RetParamUnspeakableName)
                {
                    if (invocation.Method.ReturnType == typeof(AutomationElement))
                        value = AutomationElement.Wrap((IUIAutomationElement)value);
                    invocation.ReturnValue = value;
                }
                else
                {
                    var idx = methodHelper.GetProviderMethodArgumentIndex(desc.Name);
                    if (invocation.Method.GetParameters()[idx].ParameterType.GetElementType() == typeof(AutomationElement))
                        value = AutomationElement.Wrap((IUIAutomationElement)value);
                    invocation.Arguments[idx] = value;
                }
            }
        }
 // Call a pattern instance method with this parameter list
 protected void CallMethod(UiaMethodInfoHelper methodInfo, UiaParameterListHelper paramList)
 {
     PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);
 }
 public void DispatchCallToProvider(object provider, UiaParameterListHelper paramList)
 {
     if (_getterFromProvider == null)
         throw new InvalidOperationException("You have to provide getterFromProvider lambda argument to constructor in order to use this method");
     if (paramList.Count != 1)
         throw new ArgumentException("For a property param list should contain only one out param");
     paramList[0] = _getterFromProvider(provider);
 }
 // Call a pattern instance method with this parameter list
 protected void CallMethod(UiaMethodInfoHelper methodInfo, UiaParameterListHelper paramList)
 {
     PatternInstance.CallMethod(methodInfo.Index, paramList.Data, paramList.Count);
 }
        public void PassStringParam(string value, out string retVal)
        {
            // Create and init a parameter list
            // We can't just use the CallMethod helper because we have out-parameters
            var paramList = new UiaParameterListHelper(TestSchema.GetInstance().PassStringParamMethod);
            paramList[0] = value;

            // Call through
            PatternInstance.CallMethod(TestSchema.GetInstance().PassStringParamMethod.Index, paramList.Data, paramList.Count);

            // Get the out-parameter
            retVal = (string) paramList[1];
        }
        public void DispatchCallToProvider(object provider, UiaParameterListHelper paramList)
        {
            if (ProviderMethodInfo == null)
                throw new InvalidOperationException("You need to pass providerMethodInfo if you want to call ISchemaMember.DispatchCallToProvider");
            if (paramList.Count != PatternMethodParamDescriptions.Count)
            {
                var message = string.Format("Provided paramList has unexpected number of elements. Expected {0} but was {1}",
                                            PatternMethodParamDescriptions.Count,
                                            paramList.Count);
                throw new ArgumentException(message, "paramList");
            }

            var providerCallParameters = new object[ProviderMethodInfo.GetParameters().Length];
            // fill in params
            for (int i = 0; i < PatternMethodParamDescriptions.Count; i++)
            {
                var desc = PatternMethodParamDescriptions[i];
                if (UiaTypesHelper.IsInType(desc.UiaType))
                {
                    var providerMethodParamIdx = _providerMethodInfoIndicies[desc.Name];
                    providerCallParameters[providerMethodParamIdx] = paramList[i];
                }
            }

            // call provider
            object result = null;
            try
            {
                result = ProviderMethodInfo.Invoke(provider, providerCallParameters);
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException != null)
                    throw e.InnerException;
                throw;
            }

            // write back out params
            for (int i = 0; i < PatternMethodParamDescriptions.Count; i++)
            {
                var desc = PatternMethodParamDescriptions[i];
                if (desc.Name == UiaTypesHelper.RetParamUnspeakableName)
                {
                    paramList[i] = result;
                    continue;
                }
                if (UiaTypesHelper.IsOutType(desc.UiaType))
                {
                    var providerMethodParamIdx = _providerMethodInfoIndicies[desc.Name];
                    paramList[i] = providerCallParameters[providerMethodParamIdx];
                }
            }
        }