Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ dotnet_diagnostic.IDE0017.severity = silent
dotnet_diagnostic.IDE0018.severity = silent

# IDE0019: InlineAsTypeCheck
dotnet_diagnostic.IDE0019.severity = silent
dotnet_diagnostic.IDE0019.severity = warning

# IDE0020: InlineIsTypeCheck
dotnet_diagnostic.IDE0020.severity = silent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,14 @@ protected virtual void SubscribeToCimSessionProxyEvent(CimSessionProxy proxy)
/// <returns></returns>
protected object GetBaseObject(object value)
{
PSObject psObject = value as PSObject;
if (psObject == null)
if (value is not PSObject psObject)
{
return value;
}
else
{
object baseObject = psObject.BaseObject;
var arrayObject = baseObject as object[];
if (arrayObject == null)
if (baseObject is not object[] arrayObject)
{
return baseObject;
}
Expand All @@ -384,8 +382,7 @@ protected object GetBaseObject(object value)
/// <returns>The object.</returns>
protected object GetReferenceOrReferenceArrayObject(object value, ref CimType referenceType)
{
PSReference cimReference = value as PSReference;
if (cimReference != null)
if (value is PSReference cimReference)
{
object baseObject = GetBaseObject(cimReference.Value);
if (!(baseObject is CimInstance cimInstance))
Expand All @@ -398,8 +395,7 @@ protected object GetReferenceOrReferenceArrayObject(object value, ref CimType re
}
else
{
object[] cimReferenceArray = value as object[];
if (cimReferenceArray == null)
if (value is not object[] cimReferenceArray)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ protected static string GetQuery(CimBaseCommand cmdlet)
internal static bool IsClassNameQuerySet(CimBaseCommand cmdlet)
{
DebugHelper.WriteLogEx();
GetCimInstanceCommand cmd = cmdlet as GetCimInstanceCommand;
if (cmd != null)
if (cmdlet is GetCimInstanceCommand cmd)
{
if (cmd.QueryDialect != null || cmd.SelectProperties != null || cmd.Filter != null)
{
Expand All @@ -300,8 +299,7 @@ internal static bool IsClassNameQuerySet(CimBaseCommand cmdlet)
protected static string CreateQuery(CimBaseCommand cmdlet)
{
DebugHelper.WriteLogEx();
GetCimInstanceCommand cmd = cmdlet as GetCimInstanceCommand;
if (cmd != null)
if (cmdlet is GetCimInstanceCommand cmd)
{
StringBuilder propertyList = new StringBuilder();
if (cmd.SelectProperties == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,11 @@ private void NewSubscriptionResultHandler(object src, CimSubscriptionEventArgs a
if (temp != null)
{
// raise the event
CimSubscriptionResultEventArgs resultArgs = args as CimSubscriptionResultEventArgs;
if (resultArgs != null)
if (args is CimSubscriptionResultEventArgs resultArgs)
temp(this, new CimIndicationEventInstanceEventArgs(resultArgs.Result));
else
{
CimSubscriptionExceptionEventArgs exceptionArgs = args as CimSubscriptionExceptionEventArgs;
if (exceptionArgs != null)
if (args is CimSubscriptionExceptionEventArgs exceptionArgs)
temp(this, new CimIndicationEventExceptionEventArgs(exceptionArgs.Exception));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ internal void GetCimInstance(CimInstance cimInstance, XOperationContextBase cont
{
DebugHelper.WriteLogEx();

CimNewCimInstanceContext newCimInstanceContext = context as CimNewCimInstanceContext;
if (newCimInstanceContext == null)
if (context is not CimNewCimInstanceContext newCimInstanceContext)
{
DebugHelper.WriteLog("Invalid (null) CimNewCimInstanceContext", 1);
return;
Expand Down Expand Up @@ -296,8 +295,7 @@ private CimInstance CreateCimInstance(

DebugHelper.WriteLog("Create and add new property to ciminstance: name = {0}; value = {1}; flags = {2}", 5, propertyName, propertyValue, flag);

PSReference cimReference = propertyValue as PSReference;
if (cimReference != null)
if (propertyValue is PSReference cimReference)
{
CimProperty newProperty = CimProperty.Create(propertyName, GetBaseObject(cimReference.Value), CimType.Reference, flag);
cimInstance.CimInstanceProperties.Add(newProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ private void CimIndicationHandler(object cimSession, CmdletActionEventArgs actio
}

// NOTES: should move after this.Disposed, but need to log the exception
CimWriteError cimWriteError = actionArgs.Action as CimWriteError;
if (cimWriteError != null)
if (actionArgs.Action is CimWriteError cimWriteError)
{
this.exception = cimWriteError.Exception;
if (!this.ackedEvent.IsSet)
Expand All @@ -239,11 +238,9 @@ private void CimIndicationHandler(object cimSession, CmdletActionEventArgs actio
DebugHelper.WriteLog("Got an exception: {0}", 2, exception);
}

CimWriteResultObject cimWriteResultObject = actionArgs.Action as CimWriteResultObject;
if (cimWriteResultObject != null)
if (actionArgs.Action is CimWriteResultObject cimWriteResultObject)
{
CimSubscriptionResult result = cimWriteResultObject.Result as CimSubscriptionResult;
if (result != null)
if (cimWriteResultObject.Result is CimSubscriptionResult result)
{
EventHandler<CimSubscriptionEventArgs> temp = this.OnNewSubscriptionResult;
if (temp != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ public override void OnNext(CimMethodResultBase value)

string resultObjectPSType = null;
PSObject resultObject = null;
CimMethodResult methodResult = value as CimMethodResult;
if (methodResult != null)
if (value is CimMethodResult methodResult)
{
resultObjectPSType = PSTypeCimMethodResult;
resultObject = new PSObject();
Expand All @@ -449,8 +448,7 @@ public override void OnNext(CimMethodResultBase value)
}
else
{
CimMethodStreamedResult methodStreamedResult = value as CimMethodStreamedResult;
if (methodStreamedResult != null)
if (value is CimMethodStreamedResult methodStreamedResult)
{
resultObjectPSType = PSTypeCimMethodStreamedResult;
resultObject = new PSObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ internal static ErrorRecord ErrorRecordFromAnyException(
{
Debug.Assert(inner != null, "Caller should verify inner != null");

CimException cimException = inner as CimException;
if (cimException != null)
if (inner is CimException cimException)
{
return CreateFromCimException(context, cimException, cimResultContext);
}

var containsErrorRecord = inner as IContainsErrorRecord;
if (containsErrorRecord != null)
if (inner is IContainsErrorRecord containsErrorRecord)
{
return InitializeErrorRecord(context,
exception: inner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ public override void WriteObject(object sendToPipeline, XOperationContextBase co

if (sendToPipeline is CimInstance)
{
CimSetCimInstanceContext setContext = context as CimSetCimInstanceContext;
if (setContext != null)
if (context is CimSetCimInstanceContext setContext)
{
if (string.Equals(setContext.ParameterSetName, CimBaseCommand.QueryComputerSet, StringComparison.OrdinalIgnoreCase) ||
string.Equals(setContext.ParameterSetName, CimBaseCommand.QuerySessionSet, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ private static void newSubscriber_Unsubscribed(
{
DebugHelper.WriteLogEx();

CimIndicationWatcher watcher = sender as CimIndicationWatcher;
if (watcher != null)
if (sender is CimIndicationWatcher watcher)
{
watcher.Stop();
}
Expand Down
21 changes: 7 additions & 14 deletions src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,7 @@ private string BuildStructuredQueryFromHashTable(EventLogSession eventLogSession
//
// Build xpath for <Suppress>
//
Hashtable suppresshash = hash[hashkey_supress_lc] as Hashtable;
if (suppresshash != null)
if (hash[hashkey_supress_lc] is Hashtable suppresshash)
{
xpathStringSuppress = BuildXPathFromHashTable(suppresshash);
}
Expand Down Expand Up @@ -1350,8 +1349,7 @@ private string BuildStructuredQueryFromHashTable(EventLogSession eventLogSession
private string HandleEventIdHashValue(object value)
{
StringBuilder ret = new StringBuilder();
Array idsArray = value as Array;
if (idsArray != null)
if (value is Array idsArray)
{
ret.Append('(');
for (int i = 0; i < idsArray.Length; i++)
Expand Down Expand Up @@ -1380,8 +1378,7 @@ private string HandleEventIdHashValue(object value)
private string HandleLevelHashValue(object value)
{
StringBuilder ret = new StringBuilder();
Array levelsArray = value as Array;
if (levelsArray != null)
if (value is Array levelsArray)
{
ret.Append('(');
for (int i = 0; i < levelsArray.Length; i++)
Expand Down Expand Up @@ -1412,8 +1409,7 @@ private string HandleKeywordHashValue(object value)
Int64 keywordsMask = 0;
Int64 keywordLong = 0;

Array keywordArray = value as Array;
if (keywordArray != null)
if (value is Array keywordArray)
{
foreach (object keyword in keywordArray)
{
Expand Down Expand Up @@ -1568,8 +1564,7 @@ private string HandleEndTimeHashValue(object value, Hashtable hash)
private string HandleDataHashValue(object value)
{
StringBuilder ret = new StringBuilder();
Array dataArray = value as Array;
if (dataArray != null)
if (value is Array dataArray)
{
ret.Append('(');
for (int i = 0; i < dataArray.Length; i++)
Expand Down Expand Up @@ -1599,8 +1594,7 @@ private string HandleDataHashValue(object value)
private string HandleNamedDataHashValue(string key, object value)
{
StringBuilder ret = new StringBuilder();
Array dataArray = value as Array;
if (dataArray != null)
if (value is Array dataArray)
{
ret.Append('(');
for (int i = 0; i < dataArray.Length; i++)
Expand Down Expand Up @@ -1847,8 +1841,7 @@ private void CheckHashTablesForNullValues()
}
else
{
Array eltArray = value as Array;
if (eltArray != null)
if (value is Array eltArray)
{
foreach (object elt in eltArray)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,7 @@ private IEnumerable<TSession> GetSessionsToActAgainst(QueryBuilder queryBuilder)
return this.Session;
}

var sessionBoundQueryBuilder = queryBuilder as ISessionBoundQueryBuilder<TSession>;
if (sessionBoundQueryBuilder != null)
if (queryBuilder is ISessionBoundQueryBuilder<TSession> sessionBoundQueryBuilder)
{
TSession sessionOfTheQueryBuilder = sessionBoundQueryBuilder.GetTargetSession();
if (sessionOfTheQueryBuilder != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,14 @@ internal static CimJobException CreateFromAnyException(
Dbg.Assert(jobContext != null, "Caller should verify jobContext != null");
Dbg.Assert(inner != null, "Caller should verify inner != null");

CimException cimException = inner as CimException;
if (cimException != null)
if (inner is CimException cimException)
{
return CreateFromCimException(jobDescription, jobContext, cimException);
}

string message = BuildErrorMessage(jobDescription, jobContext, inner.Message);
CimJobException cimJobException = new CimJobException(message, inner);
var containsErrorRecord = inner as IContainsErrorRecord;
if (containsErrorRecord != null)
if (inner is IContainsErrorRecord containsErrorRecord)
{
cimJobException.InitializeErrorRecord(
jobContext,
Expand Down Expand Up @@ -362,8 +360,7 @@ internal bool IsTerminatingError
{
get
{
var cimException = this.InnerException as CimException;
if ((cimException == null) || (cimException.ErrorData == null))
if ((this.InnerException is not CimException cimException) || (cimException.ErrorData == null))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ private void ProcessOutParameter(CimMethodResult methodResult, MethodParameter m
methodParameter.Value = dotNetValue;
cmdletOutput.Add(methodParameter.Name, methodParameter);

var cimInstances = dotNetValue as CimInstance[];
if (cimInstances != null)
if (dotNetValue is CimInstance[] cimInstances)
{
foreach (var instance in cimInstances)
{
CimCmdletAdapter.AssociateSessionOfOriginWithInstance(instance, this.JobContext.Session);
}
}

var cimInstance = dotNetValue as CimInstance;
if (cimInstance != null)
if (dotNetValue is CimInstance cimInstance)
{
CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance, this.JobContext.Session);
}
Expand Down Expand Up @@ -191,15 +189,13 @@ public override void OnNext(CimMethodResultBase item)
this.ExceptionSafeWrapper(
delegate
{
var methodResult = item as CimMethodResult;
if (methodResult != null)
if (item is CimMethodResult methodResult)
{
this.OnNext(methodResult);
return;
}

var streamedResult = item as CimMethodStreamedResult;
if (streamedResult != null)
if (item is CimMethodStreamedResult streamedResult)
{
this.OnNext(streamedResult);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ private static int GetNumberOfSessions(InvocationInfo invocationInfo)
int maxNumberOfSessionsIndicatedByCimInstanceArguments = 1;
foreach (object cmdletArgument in invocationInfo.BoundParameters.Values)
{
CimInstance[] array = cmdletArgument as CimInstance[];
if (array != null)
if (cmdletArgument is CimInstance[] array)
{
int numberOfSessionsAssociatedWithArgument = array
.Select(CimCmdletAdapter.GetSessionOfOriginFromCimInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ internal void ReportJobFailure(IContainsErrorRecord exception)
}
else
{
CimJobException cje = exception as CimJobException;
if ((cje != null) && (cje.IsTerminatingError))
if ((exception is CimJobException cje) && (cje.IsTerminatingError))
{
terminatingErrorTracker.MarkSessionAsTerminated(this.JobContext.Session, out sessionWasAlreadyTerminated);
isThisTerminatingError = true;
Expand Down Expand Up @@ -1019,8 +1018,7 @@ internal static bool IsShowComputerNameMarkerPresent(CimInstance cimInstance)

internal static void AddShowComputerNameMarker(PSObject pso)
{
PSPropertyInfo psShowComputerNameProperty = pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] as PSPropertyInfo;
if (psShowComputerNameProperty != null)
if (pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] is PSPropertyInfo psShowComputerNameProperty)
{
psShowComputerNameProperty.Value = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public NotFoundError(string propertyName, object propertyValue, bool wildcardsEn

if (wildcardsEnabled)
{
var propertyValueAsString = propertyValue as string;
if ((propertyValueAsString != null) && (WildcardPattern.ContainsWildcardCharacters(propertyValueAsString)))
if ((propertyValue is string propertyValueAsString) && (WildcardPattern.ContainsWildcardCharacters(propertyValueAsString)))
{
this.ErrorMessageGenerator =
(queryDescription, className) => GetErrorMessageForNotFound_ForWildcard(this.PropertyName, this.PropertyValue, className);
Expand Down Expand Up @@ -466,8 +465,7 @@ protected override BehaviorOnNoMatch GetDefaultBehaviorWhenNoMatchesFound(object
}
else
{
string expectedPropertyValueAsString = cimTypedExpectedPropertyValue as string;
if (expectedPropertyValueAsString != null && WildcardPattern.ContainsWildcardCharacters(expectedPropertyValueAsString))
if (cimTypedExpectedPropertyValue is string expectedPropertyValueAsString && WildcardPattern.ContainsWildcardCharacters(expectedPropertyValueAsString))
{
return BehaviorOnNoMatch.SilentlyContinue;
}
Expand Down Expand Up @@ -504,8 +502,7 @@ private static bool NonWildcardEqual(string propertyName, object actualPropertyV
actualPropertyValue = actualPropertyValue.ToString();
}

var expectedPropertyValueAsString = expectedPropertyValue as string;
if (expectedPropertyValueAsString != null)
if (expectedPropertyValue is string expectedPropertyValueAsString)
{
var actualPropertyValueAsString = (string)actualPropertyValue;
return actualPropertyValueAsString.Equals(expectedPropertyValueAsString, StringComparison.OrdinalIgnoreCase);
Expand Down
Loading