Using PowerShell 5.1.14393.3866 on Windows 10, I am trying to define and use custom attributes on Powershell class properties as in the following:
class MarkedAttribute : System.Attribute { }
class Person {
[Marked()]
[string]$Name
}
$p = [Person]::new()
$p.Name = "Hoho"
Write-Host $p.Name
When I run the previous script I get the following error:
At C:\Users\xxxxxxxx\Desktop\FinishLine\issue.ps1:6 char:6
+ [Marked()]
+ ~~~~~~
Cannot find the type for custom attribute 'Marked'. Make sure that the assembly that contains this type is loaded.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : CustomAttributeTypeNotFound
It is as if the custom attribute was never defined.
I am able to use 'standard' attributes such as the ones defined in System.ComponentModel but no chance with custom attributes.
I tried the following without success:
- Using the full attribute name
[MarkedAttribute()] - Defining the attribute in C# and executing
Add-Type -TypeDefinition $theCsCode
According to some posts on the web, it should be possible to use custom attribute from PowerShell.
What am I missing?