MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#nullable enable5 6using Dbg = System.Management.Automation.Diagnostics;7 8namespace System.Management.Automation9{10 /// <summary>11 /// A class representing a name that is qualified by the PSSnapin name.12 /// </summary>13 internal sealed class PSSnapinQualifiedName14 {15 private PSSnapinQualifiedName(string[] splitName)16 {17 Dbg.Assert(splitName != null, "splitName should not be null");18 Dbg.Assert(splitName.Length == 1 || splitName.Length == 2, "splitName should contain 1 or 2 elements");19 20 if (splitName.Length == 1)21 {22 _shortName = splitName[0];23 }24 else if (splitName.Length == 2)25 {26 if (!string.IsNullOrEmpty(splitName[0]))27 {28 _psSnapinName = splitName[0];29 }30 31 _shortName = splitName[1];32 }33 else34 {35 // Since the provider name contained multiple slashes it is36 // a bad format.37 38 throw PSTraceSource.NewArgumentException("name");39 }40 41 // Now set the full name42 43 if (!string.IsNullOrEmpty(_psSnapinName))44 {45 _fullName =46 string.Format(47 System.Globalization.CultureInfo.InvariantCulture,48 "{0}\\{1}",49 _psSnapinName,50 _shortName);51 }52 else53 {54 _fullName = _shortName;55 }56 }57 58 /// <summary>59 /// Gets an instance of the Name class.60 /// </summary>61 /// <param name="name">62 /// The name of the command.63 /// </param>64 /// <returns>65 /// An instance of the Name class.66 /// </returns>67 internal static PSSnapinQualifiedName? GetInstance(string? name)68 {69 if (name == null)70 return null;71 string[] splitName = name.Split('\\');72 if (splitName.Length == 0 || splitName.Length > 2)73 return null;74 var result = new PSSnapinQualifiedName(splitName);75 // If the shortname is empty, then return null...76 if (string.IsNullOrEmpty(result.ShortName))77 {78 return null;79 }80 81 return result;82 }83 84 /// <summary>85 /// Gets the command's full name.86 /// </summary>87 internal string FullName88 {89 get90 {91 return _fullName;92 }93 }94 95 private readonly string _fullName;96 97 /// <summary>98 /// Gets the command's PSSnapin name.99 /// </summary>100 internal string? PSSnapInName101 {102 get103 {104 return _psSnapinName;105 }106 }107 108 private readonly string? _psSnapinName;109 110 /// <summary>111 /// Gets the command's short name.112 /// </summary>113 internal string ShortName114 {115 get116 {117 return _shortName;118 }119 }120 121 private readonly string _shortName;122 123 /// <summary>124 /// The full name.125 /// </summary>126 /// <returns>127 /// A string representing the full name.128 /// </returns>129 public override string ToString()130 {131 return _fullName;132 }133 }134}135 