Edit

AvoidInvokingEmptyMembers

Severity Level: Warning

Default state: Always enabled

Description

This rule detects expressions where the code uses the member-access operator (.) where the member name is dynamically constructed. Invoking dynamically constructed member names can introduce unexpected behavior and runtime errors. Ensure that member invocations use constant, literal member names rather than expressions that are evaluated at runtime.

Replace dynamic member name expressions with constant member names for the target type or class.

Example

Noncompliant

$MyString = 'abc'
$MyString.('len'+'gth')

Compliant

$MyString = 'abc'
$MyString.('length')

Configure rule

This rule is always enabled and isn't configurable. Use one of the following methods to avoid using this rule:

  • Create a custom rule configuration file to include only the rules you want or exclude the rules you don't want.
  • Add the appropriate rule suppression attributes to your code to suppress the rule for specific code blocks. For more information, see the Suppressing rules section of Using PSScriptAnalyzer.