Skip to content

Error trying to use XmlSerializer with PS5-styled classes #5337

Closed

Description

Steps to reproduce

Consider that I want to serialize this simple PS5-styled class to XML:

class TelegramSettings {
    [bool] $EnableTelegramNotification
}

function Serialize-TelegramSettings($settings) {
    $type = [TelegramSettings]
    $serializer = New-Object Xml.Serialization.XmlSerializer $type
    $writer = New-Object IO.StringWriter
    try {
        $serializer.Serialize($writer, $settings)
        $writer.ToString()
    } finally {
        $writer.Dispose()
    }
}

$settings = [TelegramSettings]::new()
Serialize-TelegramSettings $settings

Expected behavior

<?xml version="1.0" encoding="utf-16"?>
<TelegramSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <EnableTelegramNotification>false</EnableTelegramNotification>
</TelegramSettings>

That could be achieved with Add-Type and C# snippet:

$source = @"
namespace Telegram
{
    public class TelegramSettings
    {
        public bool EnableTelegramNotification { get; set; }
    }
}
"@

$TelegramSettings = Add-Type -TypeDefinition $source -PassThru

function Serialize-TelegramSettings($settings) {
  $type = $TelegramSettings
  $serializer = New-Object Xml.Serialization.XmlSerializer $type
  $writer = New-Object IO.StringWriter
  try {
      $serializer.Serialize($writer, $settings)
      $writer.ToString()
  } finally {
      $writer.Dispose()
  }
}

$settings = New-Object $TelegramSettings
Serialize-TelegramSettings $settings

As you can see, my Serialize-TelegramSettings is likely right.

Actual behavior

New-Object : Exception calling ".ctor" with "1" argument(s): "A non-collectible assembly may not reference a collectible assembly."
At Test1.ps1:7 char:19
+     $serializer = New-Object Xml.Serialization.XmlSerializer $type
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand```

It looks like there's a difference between dynamically-generated assembly created by Add-Type and the one created by a class definition, and I couldn't found any settings to change the class behavior to create a non-collectible assembly or fix the issue somehow else.

Environment data

Verified on both v6.0.0-beta.7 and v5.1:

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.7
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.15063.674
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.674
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. lzybkr commented on Nov 4, 2017

    @lzybkr
    Contributor

    I'm not sure we want non-collectible assemblies, but if you want to experiment, you can change RunAndCollect to Run here.

  2. added
    Issue-Discussion
    on Nov 9, 2017
  3. ForNeVeR commented on Feb 2, 2020

    @ForNeVeR
    Author
  4. iSazonov commented on Feb 2, 2020

    @iSazonov
  5. ForNeVeR commented on Feb 2, 2020

    @ForNeVeR
    Author
  6. daxian-dbw commented on Jan 27, 2023

    @daxian-dbw
    Member

    Please keep this issue open.
    If it's closed, from the meta issue #6652 it will appear that this was fixed. So, let's keep it open until it's really fixed or resolved as by-design.

  7. added
    Resolution-No Activity
    on Nov 16, 2023
  8. ForNeVeR commented on Nov 16, 2023

    @ForNeVeR
    ContributorAuthor

    The issue still reproduces on a modern PowerShell (7.3.9).

  9. removed
    Resolution-No Activity
    on Nov 16, 2023
  10. added
    Resolution-No Activity
    on May 14, 2024
  11. ForNeVeR commented on May 14, 2024

    @ForNeVeR
    ContributorAuthor

    Sadly, this still reproduces. Perhaps we shouldn't close this yet.

  12. removed
    Resolution-No Activity
    on May 14, 2024
  13. added
    Resolution-No Activity
    on Nov 11, 2024
  14. ForNeVeR commented on Nov 11, 2024

    @ForNeVeR
    ContributorAuthor

    Sorry, but I will not surrender. The issue still exists, I don't think we should close it.

  15. removed
    Resolution-No Activity
    on Nov 11, 2024
  16. added
    WG-Engine
    Needs-Triage
    on Nov 11, 2024
  17. added
    KeepOpen
    on Feb 24, 2025
  18. rkeithhill commented on Feb 24, 2025

    @rkeithhill
    Collaborator

    The Engine WG discussed this today and agreed that PowerShell classes need to continue to be defined in dynamically generated (collectible) assemblies. It is unfortunate the XML serializer does not want to serialize type from such assemblies. Consider asking for non-collectible support from the .NET team.

  19. microsoft-github-policy-service commented on Feb 24, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey Friedrich von Never (@ForNeVeR), how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

  20. removed
    Needs-Triage
    KeepOpen
    on Feb 24, 2025
  21. added
    WG-Reviewed
    KeepOpen
    on Feb 24, 2025
  22. removed
    KeepOpen
    on Feb 24, 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

    Issue-DiscussionWG-EngineWG-Reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions