예제 #1
0
 internal void SetValue(PSObject obj, object property, object value, ExecutionContext context)
 {
     if (!LanguagePrimitives.IsNull((object)obj))
     {
         if (!LanguagePrimitives.IsNull(property))
         {
             try
             {
                 if (PSObject.Base((object)obj) is IDictionary dictionary)
                 {
                     dictionary[property] = value;
                     return;
                 }
                 PSMemberInfo memberInfo = this.GetMemberInfo(obj, property, context);
                 if (memberInfo == null)
                 {
                     throw InterpreterError.NewInterpreterException(property, typeof(RuntimeException), this.NodeToken, "PropertyNotFound", property);
                 }
                 memberInfo.Value = value;
                 return;
             }
             catch (TerminateException ex)
             {
                 throw;
             }
             catch (Exception ex)
             {
                 CommandProcessorBase.CheckForSevereException(ex);
                 throw InterpreterError.NewInterpreterExceptionByMessage(typeof(RuntimeException), this.NodeToken, ex.Message, "PropertyAssignmentException", ex);
             }
         }
     }
     throw InterpreterError.NewInterpreterException(property, typeof(RuntimeException), this.NodeToken, "PropertyNotFound", property);
 }
예제 #2
0
        internal static RuntimeException NewInterpreterExceptionWithInnerException(
            object targetObject,
            Type exceptionType,
            Token errToken,
            string resourceIdAndErrorId,
            Exception innerException,
            params object[] args)
        {
            if (string.IsNullOrEmpty(resourceIdAndErrorId))
            {
                throw InterpreterError.tracer.NewArgumentException(nameof(resourceIdAndErrorId));
            }
            RuntimeException runtimeException;

            try
            {
                string message = args == null || args.Length == 0 ? ResourceManagerCache.GetResourceString("Parser", resourceIdAndErrorId) : ResourceManagerCache.FormatResourceString("Parser", resourceIdAndErrorId, args);
                runtimeException = !string.IsNullOrEmpty(message) ? InterpreterError.NewInterpreterExceptionByMessage(exceptionType, errToken, message, resourceIdAndErrorId, innerException) : InterpreterError.NewBackupInterpreterException(exceptionType, errToken, resourceIdAndErrorId, (Exception)null);
            }
            catch (InvalidOperationException ex)
            {
                runtimeException = InterpreterError.NewBackupInterpreterException(exceptionType, errToken, resourceIdAndErrorId, (Exception)ex);
            }
            catch (MissingManifestResourceException ex)
            {
                runtimeException = InterpreterError.NewBackupInterpreterException(exceptionType, errToken, resourceIdAndErrorId, (Exception)ex);
            }
            catch (FormatException ex)
            {
                runtimeException = InterpreterError.NewBackupInterpreterException(exceptionType, errToken, resourceIdAndErrorId, (Exception)ex);
            }
            runtimeException.SetTargetObject(targetObject);
            return(runtimeException);
        }
예제 #3
0
        private Attribute GetCustomAttribute()
        {
            Attribute customAttributeObject = this.GetCustomAttributeObject();

            if (customAttributeObject != null && this._namedArguments != null)
            {
                Hashtable constValue = (Hashtable)this._namedArguments.GetConstValue();
                PSObject  psObject   = LanguagePrimitives.AsPSObjectOrNull((object)customAttributeObject);
                foreach (object key in (IEnumerable)constValue.Keys)
                {
                    object obj = constValue[key];
                    try
                    {
                        PSMemberInfo member = psObject.Members[key.ToString()];
                        if (member != null)
                        {
                            member.Value = obj;
                        }
                        else
                        {
                            throw InterpreterError.NewInterpreterException((object)this.NodeToken, typeof(RuntimeException), this.NodeToken, "PropertyNotFoundForType", (object)key.ToString(), (object)customAttributeObject.GetType().ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        CommandProcessorBase.CheckForSevereException(ex);
                        throw InterpreterError.NewInterpreterExceptionByMessage(typeof(RuntimeException), this.NodeToken, ex.Message, "PropertyAssignmentException", ex);
                    }
                }
            }
            return(customAttributeObject);
        }
예제 #4
0
        private static RuntimeException NewBackupInterpreterException(
            Type exceptionType,
            Token errToken,
            string errorId,
            Exception innerException)
        {
            string message;

            if (innerException == null)
            {
                message = ResourceManagerCache.FormatResourceString("Parser", "BackupParserMessage", (object)errorId);
            }
            else
            {
                message = ResourceManagerCache.FormatResourceString("Parser", "BackupParserMessageWithException", (object)errorId, (object)innerException.Message);
            }
            return(InterpreterError.NewInterpreterExceptionByMessage(exceptionType, errToken, message, errorId, innerException));
        }
예제 #5
0
        internal static object CallMethod(IScriptExtent errorPosition, object target, string methodName, PSMethodInvocationConstraints invocationConstraints, object[] paramArray, bool callStatic, object valueToSet)
        {
            PSMethodInfo      staticCLRMember   = null;
            MethodInformation methodInformation = null;
            object            obj2 = null;
            PSObject          obj3 = null;
            Type   type;
            object obj4;

            if (LanguagePrimitives.IsNull(target))
            {
                throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "InvokeMethodOnNull", ParserStrings.InvokeMethodOnNull, new object[0]);
            }
            obj2 = PSObject.Base(target);
            obj3 = PSObject.AsPSObject(target);
            CallsiteCacheEntryFlags none = CallsiteCacheEntryFlags.None;

            if (callStatic)
            {
                none |= CallsiteCacheEntryFlags.Static;
                type  = (Type)obj2;
            }
            else
            {
                type = obj2.GetType();
            }
            if (valueToSet != AutomationNull.Value)
            {
                none |= CallsiteCacheEntryFlags.ParameterizedSetter;
            }
            if (!obj3.isDeserialized)
            {
                methodInformation = Adapter.FindCachedMethod(type, methodName, invocationConstraints, paramArray, none);
            }
            if (methodInformation == null)
            {
                if (callStatic)
                {
                    staticCLRMember = PSObject.GetStaticCLRMember(target, methodName) as PSMethod;
                }
                else
                {
                    staticCLRMember = obj3.Members[methodName] as PSMethodInfo;
                }
                if (staticCLRMember == null)
                {
                    string fullName = null;
                    if (callStatic)
                    {
                        fullName = type.FullName;
                    }
                    else
                    {
                        fullName = GetTypeFullName(target);
                    }
                    if (valueToSet == AutomationNull.Value)
                    {
                        throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "MethodNotFound", ParserStrings.MethodNotFound, new object[] { fullName, methodName });
                    }
                    throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "ParameterizedPropertyAssignmentFailed", ParserStrings.ParameterizedPropertyAssignmentFailed, new object[] { fullName, methodName });
                }
            }
            try
            {
                if (methodInformation != null)
                {
                    object[] objArray;
                    PSObject.memberResolution.WriteLine("cache hit, Calling Method: {0}", new object[] { methodInformation.methodDefinition });
                    if (valueToSet != AutomationNull.Value)
                    {
                        DotNetAdapter.ParameterizedPropertyInvokeSet(methodName, obj2, valueToSet, new MethodInformation[] { methodInformation }, paramArray, false);
                        return(valueToSet);
                    }
                    MethodInformation[] methods = new MethodInformation[] { methodInformation };
                    Adapter.GetBestMethodAndArguments(methodName, methods, paramArray, out objArray);
                    return(DotNetAdapter.AuxiliaryMethodInvoke(obj2, objArray, methodInformation, paramArray));
                }
                if (valueToSet != AutomationNull.Value)
                {
                    PSParameterizedProperty property = staticCLRMember as PSParameterizedProperty;
                    if (property == null)
                    {
                        throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), errorPosition, "ParameterizedPropertyAssignmentFailed", ParserStrings.ParameterizedPropertyAssignmentFailed, new object[] { GetTypeFullName(target), methodName });
                    }
                    property.InvokeSet(valueToSet, paramArray);
                    return(valueToSet);
                }
                PSMethod method = staticCLRMember as PSMethod;
                if (method != null)
                {
                    return(method.Invoke(invocationConstraints, paramArray));
                }
                obj4 = staticCLRMember.Invoke(paramArray);
            }
            catch (MethodInvocationException exception)
            {
                if (exception.ErrorRecord.InvocationInfo == null)
                {
                    exception.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errorPosition));
                }
                throw;
            }
            catch (RuntimeException exception2)
            {
                if (exception2.ErrorRecord.InvocationInfo == null)
                {
                    exception2.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errorPosition));
                }
                throw;
            }
            catch (FlowControlException)
            {
                throw;
            }
            catch (ScriptCallDepthException)
            {
                throw;
            }
            catch (Exception exception3)
            {
                CommandProcessorBase.CheckForSevereException(exception3);
                throw InterpreterError.NewInterpreterExceptionByMessage(typeof(RuntimeException), errorPosition, exception3.Message, "MethodInvocationException", exception3);
            }
            return(obj4);
        }