Skip to content

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

@ForNeVeR

Description

@ForNeVeR

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifWG-Enginecore PowerShell engine, interpreter, and runtimeWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions