Title: Error trying to use XmlSerializer with PS5-styled classes · Issue #5337 · PowerShell/PowerShell · GitHub
Open Graph Title: Error trying to use XmlSerializer with PS5-styled classes · Issue #5337 · PowerShell/PowerShell
X Title: Error trying to use XmlSerializer with PS5-styled classes · Issue #5337 · PowerShell/PowerShell
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] $serializ...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/5337
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error trying to use XmlSerializer with PS5-styled classes","articleBody":"Steps to reproduce\r\n------------------\r\n\r\nConsider that I want to serialize this simple PS5-styled class to XML:\r\n\r\n```powershell\r\nclass TelegramSettings {\r\n [bool] $EnableTelegramNotification\r\n}\r\n\r\nfunction Serialize-TelegramSettings($settings) {\r\n $type = [TelegramSettings]\r\n $serializer = New-Object Xml.Serialization.XmlSerializer $type\r\n $writer = New-Object IO.StringWriter\r\n try {\r\n $serializer.Serialize($writer, $settings)\r\n $writer.ToString()\r\n } finally {\r\n $writer.Dispose()\r\n }\r\n}\r\n\r\n$settings = [TelegramSettings]::new()\r\nSerialize-TelegramSettings $settings\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\n\u003c?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n\u003cTelegramSettings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n \u003cEnableTelegramNotification>false\u003c/EnableTelegramNotification>\r\n\u003c/TelegramSettings>\r\n```\r\n\r\nThat could be achieved with `Add-Type` and C# snippet:\r\n\r\n```powershell\r\n$source = @\"\r\nnamespace Telegram\r\n{\r\n public class TelegramSettings\r\n {\r\n public bool EnableTelegramNotification { get; set; }\r\n }\r\n}\r\n\"@\r\n\r\n$TelegramSettings = Add-Type -TypeDefinition $source -PassThru\r\n\r\nfunction Serialize-TelegramSettings($settings) {\r\n $type = $TelegramSettings\r\n $serializer = New-Object Xml.Serialization.XmlSerializer $type\r\n $writer = New-Object IO.StringWriter\r\n try {\r\n $serializer.Serialize($writer, $settings)\r\n $writer.ToString()\r\n } finally {\r\n $writer.Dispose()\r\n }\r\n}\r\n\r\n$settings = New-Object $TelegramSettings\r\nSerialize-TelegramSettings $settings\r\n```\r\n\r\nAs you can see, my `Serialize-TelegramSettings` is likely right.\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\nNew-Object : Exception calling \".ctor\" with \"1\" argument(s): \"A non-collectible assembly may not reference a collectible assembly.\"\r\nAt Test1.ps1:7 char:19\r\n+ $serializer = New-Object Xml.Serialization.XmlSerializer $type\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException\r\n + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand```\r\n```\r\nIt 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.\r\n\r\nEnvironment data\r\n----------------\r\n\r\nVerified on both v6.0.0-beta.7 and v5.1:\r\n\r\n```powershell\r\n> $PSVersionTable\r\nName Value\r\n---- -----\r\nPSVersion 6.0.0-beta\r\nPSEdition Core\r\nGitCommitId v6.0.0-beta.7\r\nOS Microsoft Windows 10.0.15063\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n```powershell\r\n> $PSVersionTable\r\nName Value\r\n---- -----\r\nPSVersion 5.1.15063.674\r\nPSEdition Desktop\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}\r\nBuildVersion 10.0.15063.674\r\nCLRVersion 4.0.30319.42000\r\nWSManStackVersion 3.0\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\n```","author":{"url":"https://github.com/ForNeVeR","@type":"Person","name":"ForNeVeR"},"datePublished":"2017-11-04T07:03:33Z","url":"https://github.com/PowerShell/PowerShell/issues/5337"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0a442de7-0552-ed4e-8f5d-4d56421f2dcd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A3E:3B1A3:C2C70:11D54F:6AFDC0BD |
| html-safe-nonce | b56b49d19df96ffdc16f13209ee1add986a4a96e2f079a4774302b1980d9539a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTNFOjNCMUEzOkMyQzcwOjExRDU0Rjo2QUZEQzBCRCIsInZpc2l0b3JfaWQiOiI5MTQwNjk5OTQ0MDQwOTA2OTQxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e8b79e8028e92cbb3ab84d5715c1558f0d23e64f06f2e359afdfe908a6e71d5f |
| hovercard-subject-tag | issue:271172231 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/5337/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b6da6556c01c1baa409bad17a44313a712bce6474c9adecf7f35396fbe768201/PowerShell/PowerShell/issues/5337 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b6da6556c01c1baa409bad17a44313a712bce6474c9adecf7f35396fbe768201/PowerShell/PowerShell/issues/5337 |
| og:image:alt | Steps to reproduce Consider that I want to serialize this simple PS5-styled class to XML: class TelegramSettings { [bool] $EnableTelegramNotification } function Serialize-TelegramSettings($settings... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ForNeVeR |
| hostname | github.com |
| expected-hostname | github.com |
| None | 35c6bf57da1370dc64866160540c0ee9f825f291935b3bf6ec5492b00e35f3a6 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ab48c73a2978548773f5f24717ed9e58508befdb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width