Skip to content

Classes inheriting from [System.Attribute] cannot be defined and used in the same PowerShell module #1762

Open

Description

Steps to reproduce

set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'

class TestAttribute : System.Attribute {
}
class TestClass {
[int] [TestAttribute()] $i = 5
}

S C:\Users\scbaker> Import-Module E:\tmp\Test-Module2.psm1

Import-Module : The invoked member is not supported before the type is created.
At line:1 char:1

  • Import-Module E:\tmp\Test-Module.psm1
  • CategoryInfo : NotSpecified: (:) [Import-Module], NotSupportedException
  • FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.ImportModuleCommand

Scotia Baker:

Because class names aren't visible outside their defining module, the only workaround I've found is using Add-Type. Defining the attribute in another module using PS5 class support does not work due to lexical scoping of class names.

ISSUE:

A custom attribute class cannot be defined and used within the same PowerShell module.

IMPACT:

(1) Debug time diagnosing the issue. The import failure does not say what member or type is involved, making debugging difficult.

(2) An extra module must be created to define attribute classes regardless of logical code layout.

The primary impact is in the debugging time; an extra module may be ugly but it's easy enough. A fix for the issue is of course preferred.

Activity

  1. added this to the milestone on Sep 29, 2016
  2. SteveL-MSFT commented on Sep 29, 2016

    @SteveL-MSFT
    MemberAuthor

    TFS:7336218

  3. added
    WG-Languageparser, language semantics
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    and removed
    WG-Enginecore PowerShell engine, interpreter, and runtime
    on Feb 20, 2017
  4. wisemoth commented on Jan 16, 2019

    @wisemoth

    In advance of a fix can the workaround be clarified?

    the only workaround I've found is using Add-Type (zip of files attached).

    I can't see an attachment.

    (2) An extra module must be created to define attribute classes regardless of logical code layout.

    I've tried several ways to get my [Attribute]-derived class to be used by my function without success (WindowsPowershell 5.1.17763). The only way that works for me is to paste the class definition into my shell, and import modules containing functions using the attribute class.

  5. vexx32 commented on Jan 16, 2019

    @vexx32
    Collaborator

    This is perfectly doable. I do this to great effect in PSKoans. However, it has some caveats.

    https://github.com/vexx32/PSKoans/blob/eac7d8250dfe49e85e12b7432ad687dcf5470f78/PSKoans/PSKoans.psm1#L16-L26

    You'll notice I duplicate the definition with and without the Attribute name suffix, one inheriting from the other.

    I found that unlike in C# where you can consistently drop the Attribute portion of a class name when actually using it as an attribute, you can't always do this in PS. I don't know why, but in some circumstances (e.g., class being available in global scope) you can define a class called KoanAttribute and refer to it with [Koan()]. In others, you instead have to use the actual name of the class. To get around this weirdness, I duplicate the class name with a short name and just use it to directly inherit the original class. Whichever it picks, I'll get it more or less correct. You have to be careful when examining attributes manually, if you ever need to do that, though.

    I also use this for my transform attributes in PSWordCloud:

    https://github.com/vexx32/PSWordCloud/blob/55723befc757eae27fb20dfb7dd273360c150655/PSWordCloud/Public/New-WordCloud.ps1#L7-L58

    And its use adheres to the aforementioned restriction -- its full name has to be used, not the short name lacking the Attribute suffix on the name in the definition.

    https://github.com/vexx32/PSWordCloud/blob/55723befc757eae27fb20dfb7dd273360c150655/PSWordCloud/Public/New-WordCloud.ps1#L307-L326

    Steve Lee (@SteveL-MSFT) Jason Shirk (@lzybkr) I'm not sure why this happens, but it's weirdly inconsistent. As I mentioned, if the type is made available in global scope (e.g., by including it as a ScriptsToProcess file in the module manifest), then the attribute can be referred to as [SizeTransform()] instead of [SizeTransformAttribute()]

  6. wisemoth commented on Jan 16, 2019

    @wisemoth

    Rain Sallow (/u/ta11ow) (@vexx32) Thank you so much for the detailed and super-quick response.

    I found that unlike in C# where you can consistently drop the Attribute portion of a class name when actually using it as an attribute, you can't always do this in PS.

    This. It had always bothered me how the "magic" naming worked, and I was just pondering on this further when your post appeared. Your information has really helped me.

  7. vexx32 commented on Jan 16, 2019

    @vexx32
    Collaborator

    Glad I could help!

    Yeah, the rules here are a bit funky, and the unclear errors aren't a lot of help either, unfortunately.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  12. added
    KeepOpenThe bot will ignore these and not auto-close
    and removed
    Resolution-No ActivityIssue has had no activity for 6 months or more
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    KeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions