On a powershell session I tried to invoke some System.String
methods for string literals, but them failed
I Tried
"ssz"::EndsWith("z")
but it failed with this message error : Method invocation failed because [System.String] does not contain a method named 'EndsWith'.
Then I tried with another System.String
method
"ssz"::Substring(2,1)
but it failed too with error message Method invocation failed because [System.String] does not contain a method named 'Substring'.
To explore literal members I tried
"ssz"|Get-Member
and I got entries for EndsWith
and Substring
methods. This is the output. (I marked entries for my target methods)
I have .Net (c#) experience, and I read that powershell
is the best choice to use/consume code implemented in .Net custom assemblies from OS scripts without compiling .net code. I can't figure out where is my mistake or misconception. ¿Can anyone explaint me why my code fails?
Thank you
The OS is Windows Server 2019
PowerShell Version Info
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.2931
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.2931
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
CodePudding user response:
You are completely fine. Just that the method to call is using DOT and not double colon:
Try these replacing yours:
"ssz".EndsWith("z")
"ssz".Substring(2,1)
Hope it helps :)
CodePudding user response:
That's not a static method. Static example [string]::equals('a','a')
Tab completion works after both ::
and .
. Powershell is a nice way to explore .Net.
[string]::equals # see definitions without parentheses
OverloadDefinitions
-------------------
static bool Equals(string a, string b)
static bool Equals(string a, string b, System.StringComparison comparisonType)
static bool Equals(System.Object objA, System.Object objB)
[string]::equals('a','a')
True
Set-PSReadLineKeyHandler tab complete
[string]:: # tab — static examples
Empty Concat Format IsNullOrEmpty new
Compare Copy Intern IsNullOrWhiteSpace ReferenceEquals
CompareOrdinal Equals IsInterned Join
Instance examples, including EndsWith()
'a'. # tab
Length GetType PadLeft ToChar ToLowerInvariant ToUpperInvariant
Clone GetTypeCode PadRight ToCharArray ToSByte Trim
CompareTo IndexOf Remove ToDateTime ToSingle TrimEnd
Contains IndexOfAny Replace ToDecimal ToString TrimStart
CopyTo Insert Split ToDouble ToType Chars
EndsWith IsNormalized StartsWith ToInt16 ToUInt16
Equals LastIndexOf Substring ToInt32 ToUInt32
GetEnumerator LastIndexOfAny ToBoolean ToInt64 ToUInt64
GetHashCode Normalize ToByte ToLower ToUpper
'a'.endswith
OverloadDefinitions
-------------------
bool EndsWith(string value)
bool EndsWith(string value, System.StringComparison comparisonType)
bool EndsWith(string value, bool ignoreCase, cultureinfo culture)
'abc'.endswith('Bc') # case sensitive
False
'abc'.endswith('Bc', 'OrdinalIgnoreCase')
True
'abc'.endswith('Bc', ($ignoreCase=$true), $PSCulture)
True
'abc' -like '*Bc' # or make it easy
True