I'm writing cmdlets which Get custom .NET objects.
Some of the properties of these objects are 'int' values - one of a number of constants. When displaying the value in a table, it would be better if a friendly description of the constant is returned instead of the int value.
e.g.
C:\> Get-Animal
Name AnimalType
-------------------------------
Dolly Sheep
Nelly Elephant
Mr Ed Horse
instead of
C:\> Get-Animal
Name AnimalType
-------------------------------
Dolly 1
Nelly 2
Mr Ed 3
I can think of different ways of doing this, but they all revolve around having another property to store the string representation.
E.g.
An additional string property in the C# class for my object
public class Animal
{
public int AnimalType;
}
public class Sheep : Animal
{
public string AnimalTypeStr = "Sheep";
}
An alias property in the typedata
adding a script property like this, and using Update-TypeData to use it:
<ScriptProperty>
<Name>AnimalTypeStr</Name>
<GetScriptBlock>
if ($this.AnimalType -eq [MyNamespace]::AT_SHEEP) { return "Sheep" }
if ($this.AnimalType -eq [MyNamespace]::AT_ELEPHANT) { return "Elephant" }
if ($this.AnimalType -eq [MyNamespace]::AT_HORSE) { return "Horse" }
</GetScriptBlock>
</ScriptProperty>
This seems wrong - as I don't really want to be storing more data in my classes - I just want to control their formatting.
Is there a best-practice for this?
Can I control the formatting so that the 'int's can be displayed in different ways?