How do I prevent a class from being registered twice? #27310
-
|
In the static constructor for one of my script classes, I'm using the This sometimes throws an In the static constructor, I don't seem to be able to tell whether the What is a safe way of checking whether a script class has already been registered in the current namespace within the static constructor of the class? I'd like to avoid using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
if (-not (Get-TypeData -TypeName 'MyNs.ClassName')) {
Update-TypeData -TypeName 'MyNs.ClassName' -MemberName 'Foo' -MemberType ScriptProperty -Value { … }
}No |
Beta Was this translation helpful? Give feedback.
-
|
@MukundaKatta — thank you for the clear answer! |
Beta Was this translation helpful? Give feedback.
Get-TypeDatadoesn't throw for unknown types in PS 7+ — it just returns$null. So the pattern is:No
try/catchneeded. If you're on 5.1,$nullis still returned for unknown types there — the throwing behavior might be something else in your stack.