question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerShell classes still can't create value types

See original GitHub issue

Steps to reproduce

Try to implement any interfaces that is intended to be implemented in terms of yourself, E.g. IEquatable, IComparable:

class ServerName : Object, System.IEquatable[ServerName] {
    [string]$ComputerName
    [bool] Equals([ServerName]$other) {
        return $this.ComputerName.Equals($other.ComputerName);
    }
}

Expected behavior

I should be able to write IEquatable<T> and IComparable<T> classes 

Actual behavior

A runtime compile error:

An error occurred while creating the pipeline.
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException                                                                 + FullyQualifiedErrorId : RuntimeException                                  

Environment data

Any version of PowerShell since classes were introduced, up to and including 7 Pre 4

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JustinGrotecommented, Jan 13, 2020

@SeeminglyScience thanks. Believe it or not it totally worked 😃 Full code:

using namespace Microsoft.Management.Infrastructure
class CimObserver : System.IObserver[CimInstance] {
    [void] OnNext(
        [CimInstance]$ciminstance
    ) {
        write-host -fore green $cimInstance
    }
    [void] OnError([Exception]$Exception) {throw $exception}
    [void] OnCompleted() {$this.Unsubscribe()} 
}
$query = (new-cimsession).QueryInstancesAsync('root/cimv2','WQL','SELECT * FROM Win32_ComputerSystem')
$observer = new-object CimObserver
$query.subscribe($observer)

Outputted the textual representation in green to the host.

1reaction
SeeminglySciencecommented, Jan 13, 2020

@JustinGrote Method names are case sensitive for declaration:

using namespace Microsoft.Management.Infrastructure

class CimObserver : IObserver[CimInstance] {
    [void] OnNext([CimInstance] $value) {}
    [void] OnError([Exception] $exception) {}
    [void] OnCompleted() {}
}

Though be aware that it’s likely that OnNext will be called on a thread without a default runspace. If that’s the case, the method will just throw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Powershell custom class creation question
I think the issue stems from having objects of type [node] in the current powershell session, then redefining the [node] type.
Read more >
about Classes - PowerShell
Describes how you can use classes to create your own custom types.
Read more >
PowerShell 5: Create Simple Class - Scripting Blog
Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating a simple class in Windows PowerShell 5.0 in Windows 10.
Read more >
PowerShell Classes: Getting Started
In this tutorial, you're going to learn how to get started with PowerShell classes. You'll create your first class with constructors, learn how ......
Read more >
Using Static Classes and Methods - PowerShell
This article explains how to identify and use the properties and methods of .NET static classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found