You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using custom attributes in PowerShell Class results in parsing error. #2642
changed the title [-]Using custom attributes in PowerShell Class results in meaningless error.[/-][+]Using custom attributes in PowerShell Class results in parsing error.[/+]on Nov 8, 2016
It's a runtime error because we can't resolve the type and don't yet support defining an attribute in PowerShell.
There is no reason this couldn't work though - and that is the basic syntax you'd use (though we'd require an AttributeUsage attribute on the attribute class definition.
It constructs the metadata when an attribute is applied to something.
A blob of metadata has a reference to the constructor (and implicitly the attribute type) and the positional and named arguments for the specific application of the attribute.
Ilya (@iSazonov) - I think you're getting lucky - maybe tried this in the ISE, and the attribute was defined before you ran the full script using the attribute.
I get an error with your example when run in fresh console.
Ah - right click - that's your problem. Put the repro in a file and run that.
With right click - the attribution definition was seen as one complete statement and run before parsing the next class.
If you pasted (Ctrl+v in Windows PowerShell), then the entire buffer would have been parsed like a single file and I think you'd have seen the problem.
I see Issues with the loading of modules. Maybe they have the same root and we should gather all of them under one Issue:
New-Object doesn't work with classes in nested modules #2963
Using module behavior on nested modules is inconsistent #2964
???
Activity
[-]Using custom attributes in PowerShell Class results in meaningless error.[/-][+]Using custom attributes in PowerShell Class results in parsing error.[/+]lfshr commentedon Nov 9, 2016
This appears to go deeper than custom Attributes. Custom classes defined in in dll files also do not work! Trying my best to come up with a solution.
iSazonov commentedon Feb 18, 2017
I tested the sample in Powershell Core (alpha 15) - it work without a parser error.
lzybkr commentedon Feb 18, 2017
It's a runtime error because we can't resolve the type and don't yet support defining an attribute in PowerShell.
There is no reason this couldn't work though - and that is the basic syntax you'd use (though we'd require an
AttributeUsageattribute on the attribute class definition.iSazonov commentedon Feb 18, 2017
Jason Shirk (@lzybkr) Please clarify what
DefineCustomAttributesdo in PSTypes.cs?lzybkr commentedon Feb 18, 2017
It constructs the metadata when an attribute is applied to something.
A blob of metadata has a reference to the constructor (and implicitly the attribute type) and the positional and named arguments for the specific application of the attribute.
iSazonov commentedon Feb 19, 2017
Jason Shirk (@lzybkr) Thanks!
I don't see the parser error in PS 5.1 and PS Core alfa.15
I didn't realize that we need to fix 😕
lzybkr commentedon Feb 19, 2017
Ilya (@iSazonov) - I think you're getting lucky - maybe tried this in the ISE, and the attribute was defined before you ran the full script using the attribute.
I get an error with your example when run in fresh console.
iSazonov commentedon Feb 19, 2017
Reboot my notebook. (Win 10 build 14936)
Run C:\Program Files\PowerShell\6.0.0.15\powershell.exe
Run the test (copy by right click).
No errors 😕
I don't install any development tools, SDK... on my notebook.
Powershell is installed from package downloaded from the repo.
lzybkr commentedon Feb 19, 2017
Ah - right click - that's your problem. Put the repro in a file and run that.
With right click - the attribution definition was seen as one complete statement and run before parsing the next class.
If you pasted (Ctrl+v in Windows PowerShell), then the entire buffer would have been parsed like a single file and I think you'd have seen the problem.
iSazonov commentedon Feb 20, 2017
Jason Shirk (@lzybkr) I thought that it is discovered something new.
But this is not a new problem, the direct duplication old #1762 by Steve Lee (@SteveL-MSFT) .
I see Issues with the loading of modules. Maybe they have the same root and we should gather all of them under one Issue:
New-Object doesn't work with classes in nested modules #2963
Using module behavior on nested modules is inconsistent #2964
???
lzybkr commentedon Feb 20, 2017
Agreed - this is a dupe of #1762.
The other issues you referenced may feel similar, but I don't believe they have the same root cause.
iSazonov commentedon Feb 21, 2017
Jason Shirk (@lzybkr) Thanks! Clear.