Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think you can tack on the interfaces to the psobject you're creating. However, you can create your type in a C# class that implements the appropriate interfaces and overrides the appropriate methods. You can even define this type in a here string and use Add-Type to compile it and load it into PowerShell. Then you just create that type instead of psobject. It should have all the relevant properties you're interested as well as the ability to do comparison/equality.</p> <p>Per my comment, here is how to do this in your script with minimal headache bewteen executions i.e. you only need to change the typename variable whenever you change the C# code.</p> <pre><code>$foo = "Foo" $src = @" using System; public class $foo : IComparable { public string Name {get; set;} public int Age {get; set;} public int CompareTo(object obj) { $foo other = obj as $foo; if (other == null) throw new ArgumentException("arg must be type $foo or not null"); return Age.CompareTo(other.Age); } public override bool Equals(object obj) { if (obj == null) return false; if (Object.ReferenceEquals(this, obj)) return true; $foo other = obj as $foo; if (other == null) throw new ArgumentException("arg must be type $foo or not null"); return Age.Equals(other.Age) &amp;&amp; Name.Equals(other.Name); } public override int GetHashCode() { return Age.GetHashCode() ^ Name.GetHashCode(); } public override string ToString() { return String.Format("Name: {0}, age: {1}", Name, Age); } } "@ if (![Type]::GetType($foo)) { Add-Type -TypeDefinition $src -Language CSharpVersion3 } $foo1 = New-Object $foo $foo1.Age = 47 $foo1.Name = 'Keith' $foo1 $foo2 = New-Object $foo $foo2.Age = 45 $foo2.Name = 'John' $foo2 $foo2 -gt $foo1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload