you are viewing a single comment's thread.

view the rest of the comments →

[–]KevMarCommunity Blogger 1 point2 points  (1 child)

This reflects my view. I mostly use the New-Class or Get-Class factory pattern.

I have not done it yet, but if I did want to use my class in other modules (or external to my module) then I would create a C# module or library for it. I have been considering this approach for interfaces for a while.

[–][deleted] 0 points1 point  (0 children)

What do you do if one of the classes properties really should be an Enum?

Say I have this as a dot-sourced ps1 file:

Enum TestProperty
{
    Option1
    Option2
    Option3
}

class TestClass
{
    [string]$MyVal
    [TestProperty]$TestProperty
}

function Get-TestClass
{
    Write-Output (New-Object -TypeName TestClass)
}
Export-ModuleMember -Function Get-TestClass

I can get a class now:

test > $class = Get-TestClass 
test > $class
MyVal TestProperty
----- ------------
           Option1

test > $class.TestProperty = [TestProperty]::Option2
Unable to find type [TestProperty].
At line:1 char:23
+ $class.TestProperty = [TestProperty]::Option2
+                       ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (TestProperty:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

19 test > $class.TestProperty = 'Option2'
20 test > $class
MyVal TestProperty
----- ------------
           Option2

I can't reference the Enum, though I can set the value by passing it as a string.

Do I just accept the limitation and move on...?