Repository navigation
Closed
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
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 $settingsExpected 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 $settingsAs 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.1Reacted by aetos
Metadata
Metadata
Assignees
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
Activity
lzybkr commentedon Nov 4, 2017
I'm not sure we want non-collectible assemblies, but if you want to experiment, you can change
RunAndCollecttoRunhere.ForNeVeR commentedon Feb 2, 2020
iSazonov commentedon Feb 2, 2020
ForNeVeR commentedon Feb 2, 2020
daxian-dbw commentedon Jan 27, 2023
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.
ForNeVeR commentedon Nov 16, 2023
The issue still reproduces on a modern PowerShell (7.3.9).
ForNeVeR commentedon May 14, 2024
Sadly, this still reproduces. Perhaps we shouldn't close this yet.
ForNeVeR commentedon Nov 11, 2024
Sorry, but I will not surrender. The issue still exists, I don't think we should close it.
rkeithhill commentedon Feb 24, 2025
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.
microsoft-github-policy-service commentedon Feb 24, 2025
📣 Hey Friedrich von Never (@ForNeVeR), how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback