Beispiel #1
0
        /// <summary>
        /// Sets value of this property.
        /// </summary>
        /// <param name="target">instance of the object to which to set the property value</param>
        /// <param name="setValue">value to set this property</param>
        internal void SetValue(Object target, Object setValue)
        {
            object[] propValue = new object[1];
            setValue     = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, this.Type, CultureInfo.InvariantCulture);
            propValue[0] = setValue;

            try
            {
                ComInvoker.Invoke(target as IDispatch, _dispId, propValue, null, COM.INVOKEKIND.INVOKE_PROPERTYPUT);
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sets the value of the property.
        /// </summary>
        /// <param name="target">instance of the object to which to set the property value</param>
        /// <param name="setValue">value to set this property</param>
        /// <param name="arguments">parameters to set this property.</param>
        internal void SetValue(Object target, Object setValue, Object[] arguments)
        {
            object[] newarguments;
            var      setterCollection = new Collection <int> {
                _hasSetterByRef?_setterByRefIndex : _setterIndex
            };
            var methods    = ComUtil.GetMethodInformationArray(_typeInfo, setterCollection, true);
            var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);

            var finalArguments = new object[newarguments.Length + 1];

            for (int i = 0; i < newarguments.Length; i++)
            {
                finalArguments[i] = newarguments[i];
            }
            finalArguments[newarguments.Length] = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, Type, CultureInfo.InvariantCulture);

            try
            {
                ComInvoker.Invoke(target as IDispatch,
                                  bestMethod.DispId,
                                  finalArguments,
                                  ComInvoker.GetByRefArray(bestMethod.parameters,
                                                           finalArguments.Length,
                                                           isPropertySet: true),
                                  bestMethod.InvokeKind);
                Adapter.SetReferences(finalArguments, bestMethod, arguments);
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }
        }
Beispiel #3
0
        protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
        {
            if (!(property.baseObject is ManagementBaseObject))
            {
                throw new SetValueInvocationException("CannotSetNonManagementObjectMsg", null, ExtendedTypeSystem.CannotSetNonManagementObject, new object[] { property.Name, property.baseObject.GetType().FullName, typeof(ManagementBaseObject).FullName });
            }
            if (!this.PropertyIsSettable(property))
            {
                throw new SetValueException("ReadOnlyWMIProperty", null, ExtendedTypeSystem.ReadOnlyProperty, new object[] { property.Name });
            }
            PropertyData adapterData = property.adapterData as PropertyData;

            if (convertIfPossible && (setValue != null))
            {
                Type dotNetType = GetDotNetType(adapterData);
                setValue = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, dotNetType, CultureInfo.InvariantCulture);
            }
            adapterData.Value = setValue;
        }
Beispiel #4
0
 internal void SetValue(object target, object setValue, object[] arguments)
 {
     using (ComProperty.tracer.TraceMethod())
     {
         object[]          newArguments;
         MethodInformation methodAndArguments = Adapter.GetBestMethodAndArguments(this.Name, (MethodInformation[])ComUtil.GetMethodInformationArray(this.typeInfo, new Collection <int>()
         {
             this.hasSetterByRef ? this.setterByRefIndex : this.setterIndex
         }, true), arguments, out newArguments);
         Type     type     = target.GetType();
         object[] objArray = new object[newArguments.Length + 1];
         for (int index = 0; index < newArguments.Length; ++index)
         {
             objArray[index] = newArguments[index];
         }
         objArray[newArguments.Length] = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, this.Type, (IFormatProvider)CultureInfo.InvariantCulture);
         try
         {
             type.InvokeMember(this.name, BindingFlags.IgnoreCase | BindingFlags.SetProperty, (Binder)null, target, objArray, ComUtil.GetModifiers(methodAndArguments.parameters), CultureInfo.CurrentCulture, (string[])null);
             Adapter.SetReferences(objArray, methodAndArguments, arguments);
         }
         catch (TargetInvocationException ex)
         {
             CommandProcessorBase.CheckForSevereException(ex.InnerException);
             if (ex.InnerException is COMException innerException && innerException.ErrorCode == -2147352573)
             {
                 return;
             }
             throw;
         }
         catch (COMException ex)
         {
             if (ex.ErrorCode == -2147352570)
             {
                 return;
             }
             throw;
         }
     }
 }
Beispiel #5
0
        internal void SetValue(object target, object setValue, object[] arguments)
        {
            object[]         objArray;
            Collection <int> methods = new Collection <int> {
                (this.hasSetterByRef != null) ? this.setterByRefIndex : this.setterIndex
            };

            MethodInformation[] informationArray  = ComUtil.GetMethodInformationArray(this.typeInfo, methods, true);
            MethodInformation   methodInformation = Adapter.GetBestMethodAndArguments(this.Name, informationArray, arguments, out objArray);

            System.Type type = target.GetType();
            object[]    args = new object[objArray.Length + 1];
            for (int i = 0; i < objArray.Length; i++)
            {
                args[i] = objArray[i];
            }
            args[objArray.Length] = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, this.Type, CultureInfo.InvariantCulture);
            try
            {
                type.InvokeMember(this.name, BindingFlags.SetProperty | BindingFlags.IgnoreCase, null, target, args, ComUtil.GetModifiers(methodInformation.parameters), CultureInfo.CurrentCulture, null);
                Adapter.SetReferences(args, methodInformation, arguments);
            }
            catch (TargetInvocationException exception)
            {
                CommandProcessorBase.CheckForSevereException(exception.InnerException);
                COMException innerException = exception.InnerException as COMException;
                if ((innerException == null) || (innerException.ErrorCode != -2147352573))
                {
                    throw;
                }
            }
            catch (COMException exception3)
            {
                if (exception3.ErrorCode != -2147352570)
                {
                    throw;
                }
            }
        }
Beispiel #6
0
        private IEnumerable <string> GetTypeNameHierarchyFromDerivation(ManagementBaseObject managementObj, string dotnetBaseType, bool shouldIncludeNamespace)
        {
            StringBuilder iteratorVariable0 = new StringBuilder(200);

            iteratorVariable0.Append(dotnetBaseType);
            iteratorVariable0.Append("#");
            if (shouldIncludeNamespace)
            {
                iteratorVariable0.Append(managementObj.SystemProperties["__NAMESPACE"].Value);
                iteratorVariable0.Append(@"\");
            }
            iteratorVariable0.Append(managementObj.SystemProperties["__CLASS"].Value);
            yield return(iteratorVariable0.ToString());

            PropertyData iteratorVariable1 = managementObj.SystemProperties["__Derivation"];

            if (iteratorVariable1 != null)
            {
                string[] iteratorVariable2 = Adapter.PropertySetAndMethodArgumentConvertTo(iteratorVariable1.Value, typeof(string[]), CultureInfo.InvariantCulture) as string[];
                if (iteratorVariable2 != null)
                {
                    foreach (string iteratorVariable3 in iteratorVariable2)
                    {
                        iteratorVariable0.Clear();
                        iteratorVariable0.Append(dotnetBaseType);
                        iteratorVariable0.Append("#");
                        if (shouldIncludeNamespace)
                        {
                            iteratorVariable0.Append(managementObj.SystemProperties["__NAMESPACE"].Value);
                            iteratorVariable0.Append(@"\");
                        }
                        iteratorVariable0.Append(iteratorVariable3);
                        yield return(iteratorVariable0.ToString());
                    }
                }
            }
        }
Beispiel #7
0
 internal static object MethodArgumentConvertTo(
     object valueToConvert,
     bool isParameterByRef,
     int parameterIndex,
     Type resultType,
     IFormatProvider formatProvider)
 {
     using (PSObject.memberResolution.TraceScope("Method argument conversion."))
     {
         if (resultType == null)
         {
             throw Adapter.tracer.NewArgumentNullException(nameof(resultType));
         }
         bool isArgumentByRef;
         valueToConvert = Adapter.UnReference(valueToConvert, out isArgumentByRef);
         if (isParameterByRef && !isArgumentByRef)
         {
             throw new MethodException("NonRefArgumentToRefParameterMsg", (Exception)null, "ExtendedTypeSystem", "NonRefArgumentToRefParameter", new object[3]
             {
                 (object)(parameterIndex + 1),
                 (object)typeof(PSReference).FullName,
                 (object)"[ref]"
             });
         }
         if (isArgumentByRef && !isParameterByRef)
         {
             throw new MethodException("RefArgumentToNonRefParameterMsg", (Exception)null, "ExtendedTypeSystem", "RefArgumentToNonRefParameter", new object[3]
             {
                 (object)(parameterIndex + 1),
                 (object)typeof(PSReference).FullName,
                 (object)"[ref]"
             });
         }
         return(Adapter.PropertySetAndMethodArgumentConvertTo(valueToConvert, resultType, formatProvider));
     }
 }