Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.PowerShell.Commands
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097109", RemotingCapability = RemotingCapability.None)]
public class AddMemberCommand : PSCmdlet
{
private static readonly object s_notSpecified = new object();
private static readonly object s_notSpecified = new();

private static bool HasBeenSpecified(object obj)
{
Expand Down Expand Up @@ -221,7 +221,7 @@ private void EnsureValue1HasBeenSpecified()
{
if (!HasBeenSpecified(_value1))
{
Collection<FieldDescription> fdc = new Collection<FieldDescription>();
Collection<FieldDescription> fdc = new();
fdc.Add(new FieldDescription("Value"));
string prompt = StringUtil.Format(AddMember.Value1Prompt, _memberType);
Dictionary<string, PSObject> result = this.Host.UI.Prompt(prompt, null, fdc);
Expand Down Expand Up @@ -531,9 +531,9 @@ private void UpdateTypeNames()

private ErrorRecord NewError(string errorId, string resourceId, object targetObject, params object[] args)
{
ErrorDetails details = new ErrorDetails(this.GetType().GetTypeInfo().Assembly,
ErrorDetails details = new(this.GetType().GetTypeInfo().Assembly,
"Microsoft.PowerShell.Commands.Utility.resources.AddMember", resourceId, args);
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new InvalidOperationException(details.Message),
errorId,
ErrorCategory.InvalidOperation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public string[] Path

string[] pathValue = value;

List<string> resolvedPaths = new List<string>();
List<string> resolvedPaths = new();

// Verify that the paths are resolved and valid
foreach (string path in pathValue)
Expand Down Expand Up @@ -198,7 +198,7 @@ public string[] LiteralPath
return;
}

List<string> resolvedPaths = new List<string>();
List<string> resolvedPaths = new();
foreach (string path in value)
{
string literalPath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);
Expand Down Expand Up @@ -230,7 +230,7 @@ private void ProcessPaths(List<string> resolvedPaths)

// Throw an error if it is an unrecognized extension
default:
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(
StringUtil.Format(AddTypeStrings.FileExtensionNotSupported, currentExtension)),
"EXTENSION_NOT_SUPPORTED",
Expand All @@ -248,7 +248,7 @@ private void ProcessPaths(List<string> resolvedPaths)
else if (!string.Equals(activeExtension, currentExtension, StringComparison.OrdinalIgnoreCase))
{
// All files must have the same extension otherwise throw.
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(
StringUtil.Format(AddTypeStrings.MultipleExtensionsNotSupported)),
"MULTIPLE_EXTENSION_NOT_SUPPORTED",
Expand Down Expand Up @@ -327,7 +327,7 @@ public string OutputAssembly

// Try to resolve the path
ProviderInfo provider = null;
Collection<string> newPaths = new Collection<string>();
Collection<string> newPaths = new();

try
{
Expand All @@ -336,7 +336,7 @@ public string OutputAssembly
// Ignore the ItemNotFound -- we handle it.
catch (ItemNotFoundException) { }

ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(
StringUtil.Format(AddTypeStrings.OutputAssemblyDidNotResolve, _outputAssembly)),
"INVALID_OUTPUT_ASSEMBLY",
Expand Down Expand Up @@ -520,7 +520,7 @@ private string GetUsingTemplate(Language language)
// Generate the code for the using statements
private string GetUsingSet(Language language)
{
StringBuilder usingNamespaceSet = new StringBuilder();
StringBuilder usingNamespaceSet = new();

switch (language)
{
Expand Down Expand Up @@ -578,7 +578,7 @@ protected override void EndProcessing()
// assembly type without an output assembly
if (string.IsNullOrEmpty(_outputAssembly) && this.MyInvocation.BoundParameters.ContainsKey(nameof(OutputType)))
{
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(
string.Format(
CultureInfo.CurrentCulture,
Expand Down Expand Up @@ -630,21 +630,21 @@ protected override void EndProcessing()
private static readonly string s_frameworkFolder = PathType.GetDirectoryName(typeof(object).Assembly.Location);

// These assemblies are always automatically added to ReferencedAssemblies.
private static readonly Lazy<PortableExecutableReference[]> s_autoReferencedAssemblies = new Lazy<PortableExecutableReference[]>(InitAutoIncludedRefAssemblies);
private static readonly Lazy<PortableExecutableReference[]> s_autoReferencedAssemblies = new(InitAutoIncludedRefAssemblies);

// A HashSet of assembly names to be ignored if they are specified in '-ReferencedAssemblies'
private static readonly Lazy<HashSet<string>> s_refAssemblyNamesToIgnore = new Lazy<HashSet<string>>(InitRefAssemblyNamesToIgnore);
private static readonly Lazy<HashSet<string>> s_refAssemblyNamesToIgnore = new(InitRefAssemblyNamesToIgnore);

// These assemblies are used, when ReferencedAssemblies parameter is not specified.
private static readonly Lazy<IEnumerable<PortableExecutableReference>> s_defaultAssemblies = new Lazy<IEnumerable<PortableExecutableReference>>(InitDefaultRefAssemblies);
private static readonly Lazy<IEnumerable<PortableExecutableReference>> s_defaultAssemblies = new(InitDefaultRefAssemblies);

private bool InMemory { get { return string.IsNullOrEmpty(_outputAssembly); } }

// These dictionaries prevent reloading already loaded and unchanged code.
// We don't worry about unbounded growing of the cache because in .Net Core 2.0 we can not unload assemblies.
// TODO: review if we will be able to unload assemblies after migrating to .Net Core 2.1.
private static readonly HashSet<string> s_sourceTypesCache = new HashSet<string>();
private static readonly Dictionary<int, Assembly> s_sourceAssemblyCache = new Dictionary<int, Assembly>();
private static readonly HashSet<string> s_sourceTypesCache = new();
private static readonly Dictionary<int, Assembly> s_sourceAssemblyCache = new();

private static readonly string s_defaultSdkDirectory = Utils.DefaultPowerShellAppBase;

Expand Down Expand Up @@ -996,7 +996,7 @@ private void SourceCodeProcessing()
}

SourceText sourceText;
List<SyntaxTree> syntaxTrees = new List<SyntaxTree>();
List<SyntaxTree> syntaxTrees = new();

switch (ParameterSetName)
{
Expand Down Expand Up @@ -1075,12 +1075,12 @@ private void CompileToAssembly(List<SyntaxTree> syntaxTrees, CompilationOptions

private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag<string> newTypes)
{
AllNamedTypeSymbolsVisitor visitor = new AllNamedTypeSymbolsVisitor();
AllNamedTypeSymbolsVisitor visitor = new();
visitor.Visit(compilation.Assembly.GlobalNamespace);

foreach (var symbolName in visitor.DuplicateSymbols)
{
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(
string.Format(AddTypeStrings.TypeAlreadyExists, symbolName)),
"TYPE_ALREADY_EXISTS",
Expand All @@ -1091,7 +1091,7 @@ private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag<stri

if (!visitor.DuplicateSymbols.IsEmpty)
{
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new InvalidOperationException(AddTypeStrings.CompilerErrors),
"COMPILER_ERRORS",
ErrorCategory.InvalidData,
Expand All @@ -1107,8 +1107,8 @@ private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag<stri
// Visit symbols in all namespaces and collect duplicates.
private class AllNamedTypeSymbolsVisitor : SymbolVisitor
{
public readonly ConcurrentBag<string> DuplicateSymbols = new ConcurrentBag<string>();
public readonly ConcurrentBag<string> UniqueSymbols = new ConcurrentBag<string>();
public readonly ConcurrentBag<string> DuplicateSymbols = new();
public readonly ConcurrentBag<string> UniqueSymbols = new();

public override void VisitNamespace(INamespaceSymbol symbol)
{
Expand Down Expand Up @@ -1244,7 +1244,7 @@ private void HandleCompilerErrors(ImmutableArray<Diagnostic> compilerDiagnostics
}
else
{
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new Exception(errorText),
"SOURCE_CODE_ERROR",
ErrorCategory.InvalidData,
Expand All @@ -1256,7 +1256,7 @@ private void HandleCompilerErrors(ImmutableArray<Diagnostic> compilerDiagnostics

if (IsError)
{
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new InvalidOperationException(AddTypeStrings.CompilerErrors),
"COMPILER_ERRORS",
ErrorCategory.InvalidData,
Expand Down Expand Up @@ -1289,7 +1289,7 @@ private string BuildErrorMessage(Diagnostic diagnisticRecord)
var errorLineString = textLines[errorLineNumber].ToString();
var errorPosition = lineSpan.StartLinePosition.Character;

StringBuilder sb = new StringBuilder(diagnisticMessage.Length + errorLineString.Length * 2 + 4);
StringBuilder sb = new(diagnisticMessage.Length + errorLineString.Length * 2 + 4);

sb.AppendLine(diagnisticMessage);
sb.AppendLine(errorLineString);
Expand All @@ -1315,7 +1315,7 @@ private string BuildErrorMessage(Diagnostic diagnisticRecord)
private static int SyntaxTreeArrayGetHashCode(IEnumerable<SyntaxTree> sts)
{
// We use our extension method EnumerableExtensions.SequenceGetHashCode<T>().
List<int> stHashes = new List<int>();
List<int> stHashes = new();
foreach (var st in sts)
{
stHashes.Add(SyntaxTreeGetHashCode(st));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public SwitchParameter PassThru
private List<OrderByPropertyEntry> _referenceEntries;

private readonly List<OrderByPropertyEntry> _referenceEntryBacklog
= new List<OrderByPropertyEntry>();
= new();

private readonly List<OrderByPropertyEntry> _differenceEntryBacklog
= new List<OrderByPropertyEntry>();
= new();

private OrderByProperty _orderByProperty = null;
private OrderByPropertyComparer _comparer = null;
Expand Down Expand Up @@ -256,7 +256,7 @@ private void InitComparer()
if (_comparer != null)
return;

List<PSObject> referenceObjectList = new List<PSObject>(ReferenceObject);
List<PSObject> referenceObjectList = new(ReferenceObject);
_orderByProperty = new OrderByProperty(
this, referenceObjectList, Property, true, _cultureInfo, CaseSensitive);
Diagnostics.Assert(_orderByProperty.Comparer != null, "no comparer");
Expand Down Expand Up @@ -327,7 +327,7 @@ private void Emit(OrderByPropertyEntry entry, string sideIndicator)
mshobj = new PSObject();
if (Property == null || Property.Length == 0)
{
PSNoteProperty inputNote = new PSNoteProperty(
PSNoteProperty inputNote = new(
InputObjectPropertyName, entry.inputObject);
mshobj.Properties.Add(inputNote);
}
Expand All @@ -348,7 +348,7 @@ private void Emit(OrderByPropertyEntry entry, string sideIndicator)
object prop = hash[FormatParameterDefinitionKeys.ExpressionEntryKey];
Diagnostics.Assert(prop != null, "null prop");
string propName = prop.ToString();
PSNoteProperty propertyNote = new PSNoteProperty(
PSNoteProperty propertyNote = new(
propName,
entry.orderValues[i].PropertyValue);
try
Expand All @@ -364,7 +364,7 @@ private void Emit(OrderByPropertyEntry entry, string sideIndicator)
}

mshobj.Properties.Remove(SideIndicatorPropertyName);
PSNoteProperty sideNote = new PSNoteProperty(
PSNoteProperty sideNote = new(
SideIndicatorPropertyName, sideIndicator);
mshobj.Properties.Add(sideNote);
WriteObject(mshobj);
Expand Down Expand Up @@ -411,7 +411,7 @@ protected override void ProcessRecord()
InitComparer();
}

List<PSObject> differenceList = new List<PSObject>(DifferenceObject);
List<PSObject> differenceList = new(DifferenceObject);
List<OrderByPropertyEntry> differenceEntries =
OrderByProperty.CreateOrderMatrix(
this, differenceList, _orderByProperty.MshParameterList);
Expand Down Expand Up @@ -459,7 +459,7 @@ private void HandleDifferenceObjectOnly()
return;
}

List<PSObject> differenceList = new List<PSObject>(DifferenceObject);
List<PSObject> differenceList = new(DifferenceObject);
_orderByProperty = new OrderByProperty(
this, differenceList, Property, true, _cultureInfo, CaseSensitive);
List<OrderByPropertyEntry> differenceEntries =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ConsoleColorCmdlet()
private static ErrorRecord BuildOutOfRangeErrorRecord(object val, string errorId)
{
string msg = StringUtil.Format(HostStrings.InvalidColorErrorTemplate, val.ToString());
ArgumentOutOfRangeException e = new ArgumentOutOfRangeException("value", val, msg);
ArgumentOutOfRangeException e = new("value", val, msg);
return new ErrorRecord(e, errorId, ErrorCategory.InvalidArgument, null);
}
#endregion helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ private string ConvertToNTAccount(SecurityIdentifier securityIdentifier)

private List<string> GetApplicableAccessRights(int accessMask, AccessRightTypeNames? typeName)
{
List<Type> typesToExamine = new List<Type>();
List<string> foundAccessRightNames = new List<string>();
HashSet<int> foundAccessRightValues = new HashSet<int>();
List<Type> typesToExamine = new();
List<string> foundAccessRightNames = new();
HashSet<int> foundAccessRightValues = new();

if (typeName != null)
{
Expand Down Expand Up @@ -121,10 +121,10 @@ private string[] ConvertAccessControlListToStrings(CommonAcl acl, AccessRightTyp
return Array.Empty<string>();
}

List<string> aceStringList = new List<string>(acl.Count);
List<string> aceStringList = new(acl.Count);
foreach (CommonAce ace in acl)
{
StringBuilder aceString = new StringBuilder();
StringBuilder aceString = new();
string ntAccount = ConvertToNTAccount(ace.SecurityIdentifier);
aceString.Append($"{ntAccount}: {ace.AceQualifier}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string StringData
/// </summary>
protected override void ProcessRecord()
{
Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);
Hashtable result = new(StringComparer.OrdinalIgnoreCase);

if (string.IsNullOrEmpty(_stringData))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected override void ProcessRecord()
else
{
string errorMessage = StringUtil.Format(ConvertMarkdownStrings.InvalidInputObjectType, baseObj.GetType());
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new InvalidDataException(errorMessage),
"InvalidInputObject",
ErrorCategory.InvalidData,
Expand Down Expand Up @@ -150,7 +150,7 @@ private async Task<string> ReadContentFromFile(string filePath)

try
{
using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
using (StreamReader reader = new(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
string mdContent = await reader.ReadToEndAsync();
return mdContent;
Expand Down Expand Up @@ -189,7 +189,7 @@ private List<string> ResolvePath(string path, bool isLiteral)
{
ProviderInfo provider = null;
PSDriveInfo drive = null;
List<string> resolvedPaths = new List<string>();
List<string> resolvedPaths = new();

try
{
Expand All @@ -216,7 +216,7 @@ private List<string> ResolvePath(string path, bool isLiteral)
if (!provider.Name.Equals("FileSystem", StringComparison.OrdinalIgnoreCase))
{
string errorMessage = StringUtil.Format(ConvertMarkdownStrings.FileSystemPathsOnly, path);
ErrorRecord errorRecord = new ErrorRecord(
ErrorRecord errorRecord = new(
new ArgumentException(),
"OnlyFileSystemPathsSupported",
ErrorCategory.InvalidArgument,
Expand Down
Loading