internal void ExceptionSafeWrapper(Action action) { try { try { action(); } catch (CimJobException cimJobException1) { CimJobException cimJobException = cimJobException1; this.ReportJobFailure(cimJobException); } catch (PSInvalidCastException pSInvalidCastException1) { PSInvalidCastException pSInvalidCastException = pSInvalidCastException1; this.ReportJobFailure(pSInvalidCastException); } catch (CimException cimException1) { CimException cimException = cimException1; CimJobException cimJobException2 = CimJobException.CreateFromCimException(this.GetDescription(), this.JobContext, cimException); this.ReportJobFailure(cimJobException2); } catch (PSInvalidOperationException pSInvalidOperationException) { lock (this._jobStateLock) { bool flag = false; if (this._jobWasStopped) { flag = true; } if (this._alreadyReachedCompletedState && this._jobHadErrors) { flag = true; } if (!flag) { throw; } } } } catch (Exception exception1) { Exception exception = exception1; CimJobException cimJobException3 = CimJobException.CreateFromAnyException(this.GetDescription(), this.JobContext, exception); this.ReportJobFailure(cimJobException3); } }
public static object ConvertTo(object srcObject, Type destType) { object obj; if (srcObject != null) { if (!srcObject.GetType().IsEnum || !(destType != typeof(string))) { try { if (srcObject != null && srcObject.GetType() == typeof(string)) { if (!TypeSystem.IsNullableType(destType) || !string.IsNullOrEmpty(srcObject as string)) { if (destType == typeof(bool)) { obj = bool.Parse(srcObject as string); return(obj); } } else { obj = null; return(obj); } } obj = LanguagePrimitives.ConvertTo(srcObject, destType, CultureInfo.InvariantCulture); } catch (PSInvalidCastException pSInvalidCastException1) { PSInvalidCastException pSInvalidCastException = pSInvalidCastException1; throw new InvalidCastException(ExceptionHelpers.GetInvalidCastExceptionMessage(srcObject.GetType(), destType), pSInvalidCastException); } catch (FormatException formatException1) { FormatException formatException = formatException1; throw new InvalidCastException(ExceptionHelpers.GetInvalidCastExceptionMessage(srcObject.GetType(), destType), formatException); } return(obj); } else { throw new InvalidCastException(ExceptionHelpers.GetInvalidCastExceptionMessage(srcObject.GetType(), destType)); } } else { return(null); } }
internal static Type ResolveTypeNameWithContext(TypeName typeName, out Exception exception, Assembly[] assemblies, TypeResolutionState typeResolutionState) { ExecutionContext context = null; exception = null; if (typeResolutionState == null) { // Usings from script scope (and if no script scope, fall back to default 'using namespace system') context = LocalPipeline.GetExecutionContextFromTLS(); typeResolutionState = TypeResolutionState.GetDefaultUsingState(context); } // We can do the cache lookup only if we don't define type in the current scope (cache would be invalid in this case). var result = typeResolutionState.ContainsTypeDefined(typeName.Name) ? null : TypeCache.Lookup(typeName, typeResolutionState); if (result != null) { return(result); } if (typeName.AssemblyName != null) { result = ResolveAssemblyQualifiedTypeName(typeName, out exception); TypeCache.Add(typeName, typeResolutionState, result); return(result); } // Simple typename (no generics, no arrays, no assembly name) // We use the following search order, using the specified name (assumed to be fully namespace qualified): // // * Search scope table (includes 'using type x = ...' aliases) // * Built in type accelerators (implicit 'using type x = ...' aliases that are effectively in global scope // * typeResolutionState.assemblies, which contains: // - Assemblies with PS types, added by 'using module' // - Assemblies added by 'using assembly'. // For this case, we REPORT ambiguity, since user explicitly specifies the set of assemblies. // * All other loaded assemblies (excluding dynamic assemblies created for PS defined types). // IGNORE ambiguity. It mimics PS v4. There are two reasons: // 1) If we report ambiguity, we need to fix our caching logic accordingly. // Consider this code // Add-Type 'public class Q {}' # ok // Add-Type 'public class Q { }' # get error about the same name // [Q] # we would get error about ambiguous type, because we added assembly with duplicated type // # before we can report TYPE_ALREADY_EXISTS error. // // Add-Type 'public class Q2 {}' # ok // [Q2] # caching Q2 type // Add-Type 'public class Q2 { }' # get error about the same name // [Q2] # we don't get an error about ambiguous type, because it's cached already // 2) NuGet (VS Package Management console) uses MEF extensibility model. // Different assemblies includes same interface (i.e. NuGet.VisualStudio.IVsPackageInstallerServices), // where they include only methods that they are interested in the interface declaration (result interfaces are different!). // Then, at runtime VS provides an instance. Everything work as far as instance has compatible API. // So [NuGet.VisualStudio.IVsPackageInstallerServices] can be resolved to several different assemblies and it's ok. // * User defined type accelerators (rare - interface was never public) // // If nothing is found, we search again, this time applying any 'using namespace ...' declarations including the implicit 'using namespace System'. // We must search all using aliases and REPORT an error if there is an ambiguity. // If this is TypeDefinition we should not cache anything in TypeCache. if (typeName._typeDefinitionAst != null) { return(typeName._typeDefinitionAst.Type); } if (context == null) { context = LocalPipeline.GetExecutionContextFromTLS(); } // Use the explicitly passed-in assembly list when it's specified by the caller. // Otherwise, retrieve all currently loaded assemblies. var assemList = assemblies ?? ClrFacade.GetAssemblies(typeResolutionState, typeName); var isAssembliesExplicitlyPassedIn = assemblies != null; result = CallResolveTypeNameWorkerHelper(typeName, context, assemList, isAssembliesExplicitlyPassedIn, typeResolutionState, out exception); if (result != null) { TypeCache.Add(typeName, typeResolutionState, result); return(result); } if (exception == null) { foreach (var ns in typeResolutionState.namespaces) { var newTypeNameToSearch = ns + "." + typeName.Name; newTypeNameToSearch = typeResolutionState.GetAlternateTypeName(newTypeNameToSearch) ?? newTypeNameToSearch; var newTypeName = new TypeName(typeName.Extent, newTypeNameToSearch); #if CORECLR if (!isAssembliesExplicitlyPassedIn) { // We called 'ClrFacade.GetAssemblies' to get assemblies. That means the assemblies to search from // are not pre-defined, and thus we have to refetch assembly again based on the new type name. assemList = ClrFacade.GetAssemblies(typeResolutionState, newTypeName); } #endif var newResult = CallResolveTypeNameWorkerHelper(newTypeName, context, assemList, isAssembliesExplicitlyPassedIn, typeResolutionState, out exception); if (exception != null) { break; } if (newResult != null) { if (result == null) { result = newResult; } else { exception = new AmbiguousTypeException(typeName, new string[] { result.FullName, newResult.FullName }); result = null; break; } } } } if (exception != null) { // AmbiguousTypeException is for internal representation only. var ambiguousException = exception as AmbiguousTypeException; if (ambiguousException != null) { exception = new PSInvalidCastException("AmbiguousTypeReference", exception, ParserStrings.AmbiguousTypeReference, ambiguousException.TypeName.Name, ambiguousException.Candidates[0], ambiguousException.Candidates[1]); } } if (result != null) { TypeCache.Add(typeName, typeResolutionState, result); } return(result); }
private void ParseHelper(string[] args) { bool flag = false; for (int i = 0; i < (int)args.Length; i++) { string lowerInvariant = args[i].Trim().ToLowerInvariant(); if (!string.IsNullOrEmpty(lowerInvariant)) { if (SpecialCharacters.IsDash(lowerInvariant[0]) || lowerInvariant[0] == '/') { lowerInvariant = lowerInvariant.Substring(1); if (this.MatchSwitch(lowerInvariant, "help", "h") || this.MatchSwitch(lowerInvariant, "?", "?")) { this.showHelp = true; this.abortStartup = true; } else { if (!this.MatchSwitch(lowerInvariant, "noexit", "noe")) { if (!this.MatchSwitch(lowerInvariant, "importsystemmodules", "imp")) { if (!this.MatchSwitch(lowerInvariant, "showinitialprompt", "show")) { if (!this.MatchSwitch(lowerInvariant, "noprofile", "nop")) { if (!this.MatchSwitch(lowerInvariant, "nologo", "nol")) { if (!this.MatchSwitch(lowerInvariant, "noninteractive", "noni")) { if (!this.MatchSwitch(lowerInvariant, "servermode", "s")) { if (!this.MatchSwitch(lowerInvariant, "command", "c")) { if (!this.MatchSwitch(lowerInvariant, "windowstyle", "w")) { if (!this.MatchSwitch(lowerInvariant, "file", "f")) { if (this.MatchSwitch(lowerInvariant, "outputformat", "o") || this.MatchSwitch(lowerInvariant, "of", "o")) { this.ParseFormat(args, ref i, ref this.outFormat, CommandLineParameterParserStrings.MissingOutputFormatParameter); } else { if (this.MatchSwitch(lowerInvariant, "inputformat", "i") || this.MatchSwitch(lowerInvariant, "if", "i")) { this.ParseFormat(args, ref i, ref this.inFormat, CommandLineParameterParserStrings.MissingInputFormatParameter); } else { if (this.MatchSwitch(lowerInvariant, "executionpolicy", "ex") || this.MatchSwitch(lowerInvariant, "ep", "ep")) { this.ParseExecutionPolicy(args, ref i, ref this.executionPolicy, CommandLineParameterParserStrings.MissingExecutionPolicyParameter); } else { if (this.MatchSwitch(lowerInvariant, "encodedcommand", "e") || this.MatchSwitch(lowerInvariant, "ec", "e")) { this.wasCommandEncoded = true; if (!this.ParseCommand(args, ref i, flag, true)) { break; } } else { if (this.MatchSwitch(lowerInvariant, "encodedarguments", "encodeda") || this.MatchSwitch(lowerInvariant, "ea", "ea")) { if (!this.CollectArgs(args, ref i)) { break; } } else { if (!this.MatchSwitch(lowerInvariant, "sta", "s")) { if (!this.MatchSwitch(lowerInvariant, "mta", "mta")) { i--; if (!this.ParseCommand(args, ref i, flag, false)) { break; } } else { if (!this.staMode.HasValue) { this.staMode = new bool?(false); } else { this.ui.WriteErrorLine(CommandLineParameterParserStrings.MtaStaMutuallyExclusive); this.showHelp = false; this.showBanner = false; this.abortStartup = true; this.exitCode = -196608; break; } } } else { if (!this.staMode.HasValue) { this.staMode = new bool?(true); } else { this.ui.WriteErrorLine(CommandLineParameterParserStrings.MtaStaMutuallyExclusive); this.showHelp = false; this.showBanner = false; this.abortStartup = true; this.exitCode = -196608; break; } } } } } } } } else { i++; if (i < (int)args.Length) { if (!flag) { this.showBanner = false; } if (!flag) { this.noExit = false; } if (args[i] != "-") { string message = null; try { this.file = Path.GetFullPath(args[i]); } catch (Exception exception1) { Exception exception = exception1; ConsoleHost.CheckForSevereException(exception); message = exception.Message; } if (message == null) { if (Path.GetExtension(this.file).Equals(".ps1", StringComparison.OrdinalIgnoreCase)) { if (System.IO.File.Exists(this.file)) { i++; Regex regex = new Regex("^.\\w+\\:", RegexOptions.CultureInvariant); string str = null; while (i < (int)args.Length) { string str1 = args[i]; if (str == null) { if (string.IsNullOrEmpty(str1) || !SpecialCharacters.IsDash(str1[0])) { this.collectedArgs.Add(new CommandParameter(null, str1)); } else { Match match = regex.Match(str1); if (!match.Success) { this.collectedArgs.Add(new CommandParameter(str1)); } else { int num = str1.IndexOf(':'); if (num != str1.Length - 1) { this.collectedArgs.Add(new CommandParameter(str1.Substring(0, num), str1.Substring(num + 1))); } else { char[] chrArray = new char[1]; chrArray[0] = ':'; str = str1.TrimEnd(chrArray); } } } } else { this.collectedArgs.Add(new CommandParameter(str, str1)); str = null; } i++; } break; } else { object[] objArray = new object[1]; objArray[0] = args[i]; this.ui.WriteErrorLine(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.ArgumentFileDoesNotExist, objArray)); this.showHelp = false; this.abortStartup = true; this.exitCode = -196608; break; } } else { object[] objArray1 = new object[1]; objArray1[0] = args[i]; this.ui.WriteErrorLine(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidFileArgumentExtension, objArray1)); this.showHelp = false; this.abortStartup = true; this.exitCode = -196608; break; } } else { object[] objArray2 = new object[2]; objArray2[0] = args[i]; objArray2[1] = message; this.ui.WriteErrorLine(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidFileArgument, objArray2)); this.showHelp = false; this.abortStartup = true; this.exitCode = -196608; break; } } else { this.readFromStdin = true; this.noPrompt = false; break; } } else { this.ui.WriteErrorLine(CommandLineParameterParserStrings.MissingFileArgument); this.showHelp = true; this.abortStartup = true; this.exitCode = -196608; break; } } } else { i++; if (i < (int)args.Length) { try { ProcessWindowStyle processWindowStyle = (ProcessWindowStyle)LanguagePrimitives.ConvertTo(args[i], typeof(ProcessWindowStyle), CultureInfo.InvariantCulture); ConsoleControl.SetConsoleMode(processWindowStyle); } catch (PSInvalidCastException pSInvalidCastException1) { PSInvalidCastException pSInvalidCastException = pSInvalidCastException1; object[] message1 = new object[2]; message1[0] = args[i]; message1[1] = pSInvalidCastException.Message; this.ui.WriteErrorLine(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidWindowStyleArgument, message1)); this.showHelp = false; this.showBanner = false; this.abortStartup = true; this.exitCode = -196608; break; } } else { this.ui.WriteErrorLine(CommandLineParameterParserStrings.MissingWindowStyleArgument); this.showHelp = false; this.showBanner = false; this.abortStartup = true; this.exitCode = -196608; break; } } } else { if (!this.ParseCommand(args, ref i, flag, false)) { break; } } } else { this.serverMode = true; } } else { this.noInteractive = true; if (ConsoleHost.DefaultInitialSessionState != null) { ConsoleHost.DefaultInitialSessionState.WarmUpTabCompletionOnIdle = false; } } } else { this.showBanner = false; } } else { this.skipUserInit = true; } } else { this.showInitialPrompt = true; } } else { this.importSystemModules = true; } } else { this.noExit = true; flag = true; } } } else { i--; this.ParseCommand(args, ref i, flag, false); break; } } } if (this.showHelp) { this.ShowHelp(); } if (this.showBanner && !this.showHelp) { this.ShowBanner(); } }