Title: Strange behaviour when using in a Powershell class a class defined in a DLL imported with Add-Type · Issue #26477 · PowerShell/PowerShell · GitHub
Open Graph Title: Strange behaviour when using in a Powershell class a class defined in a DLL imported with Add-Type · Issue #26477 · PowerShell/PowerShell
X Title: Strange behaviour when using in a Powershell class a class defined in a DLL imported with Add-Type · Issue #26477 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/26477
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Strange behaviour when using in a Powershell class a class defined in a DLL imported with Add-Type","articleBody":"### Prerequisites\n\n- [x] Write a descriptive title.\n- [x] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\n- [x] Search the existing issues.\n- [x] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\n- [x] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\n\n### Steps to reproduce\n\nI don't know if this is a bug or simply the expected behavior, but it seems strange to me.\nIn a Powershell class, I want to use a class defined in a DLL imported with Add-Type.\n\nFor my first attempt, I simply tried the ClassS1.ps1 script.\n\n# ClassS1.ps1\nAdd-Type -path '.\\obs-websocket-dotnet.dll'\n#\n[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n#\nclass ClassS1 {\n\t[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n}\n#\n\nRunning the ClassS1.ps1 script generates the following error\n\nParserError: …..\\ClassS1.ps1:7\nLine |\n 7 | [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n | Unable to find type [OBSWebsocketDotNet.OBSWebsocket].\n\nUsing the [OBSWebsocketDotNet.OBSWebsocket] class defined in obs-websocket-dotnet.dll generates an error only when used inside the powershell class definition.\n\nFor my second attempt, I tried using a powershell module hosting the powershell class definition.\n\n# ClassS2.ps1\nusing module '.\\ClassM2.psm1'\nAdd-Type -path '.\\obs-websocket-dotnet.dll'\n#\n[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n#\n\n# ClassM2.psm1\nclass ClassM2 {\n\t[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n}\n#\n\nRunning the ClassS2.ps1 script generates the following error\n\n.\\ClassS2.ps1: At D:\\Données\\Alain\\CloudStation\\Eglise\\Touch Portal\\Tests\\Classes\\ClassM2.psm1:3 char:3\n+ [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nUnable to find type [OBSWebsocketDotNet.OBSWebsocket].\n\nThis error is not surprising because the DLL is only imported after the using module statement, module that needs the [OBSWebsocketDotNet.OBSWebsocket] class.\n\nIn order to bypass the requirement to use the using statement before any other statement, I made a third attempt by placing the Add-Type statement in another AddType.psm1 module.\n\n# AddType.psm1\nAdd-Type -path '.\\obs-websocket-dotnet.dll'\n#\n\n# ClassS3.ps1\nusing module '.\\AddType.psm1'\nusing module '.\\ClassM3.psm1'\n#\n[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n#\n\n# ClassM3.psm1\nclass ClassM3 {\n\t[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n}\n#\n\nRunning the ClassS3.ps1 script does not generate any error\n\nThe fourth attempt uses only the script similar to that of the first attempt but using the AddType.psm1 module from the third attempt.\n\n# ClassS4.ps1\nusing module '.\\AddType.psm1'\n#\n[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n#\t\nclass ClassS4 {\n\t[OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n}\n#\n\nRunning the ClassS4.ps1 script does not generate any error\n\nAre these behaviors expected ?\n\n\n### Expected behavior\n\n```console\nno error when running ClassS1.ps1\n```\n\n### Actual behavior\n\n```console\nerror when running ClassS1.ps1\n\nParserError: ......\\ClassS1.ps1:7\nLine |\n 7 | [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n | Unable to find type [OBSWebsocketDotNet.OBSWebsocket].\n```\n\n### Error details\n\n```console\nException :\n Type : System.Management.Automation.ParseException\n Errors :\n Extent : OBSWebsocketDotNet.OBSWebsocket\n ErrorId : TypeNotFound\n Message : Unable to find type [OBSWebsocketDotNet.OBSWebsocket].\n Message : At ......\\ClassS1.ps1:7 char:3\n + [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n Unable to find type [OBSWebsocketDotNet.OBSWebsocket].\n ErrorRecord :\n Exception :\n Type : System.Management.Automation.ParentContainsErrorRecordException\n Message : At ......\\ClassS1.ps1:7 char:3\n + [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n Unable to find type [OBSWebsocketDotNet.OBSWebsocket].\n HResult : -2146233087\n CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException\n FullyQualifiedErrorId : TypeNotFound\n InvocationInfo :\n ScriptLineNumber : 7\n OffsetInLine : 3\n HistoryId : 2\n ScriptName : ......\\ClassS1.ps1\n Line : [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n\n Statement : OBSWebsocketDotNet.OBSWebsocket\n PositionMessage : At ......\\ClassS1.ps1:7 char:3\n + [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n PSScriptRoot : ......\\Classes\n PSCommandPath : ......\\ClassS1.ps1\n CommandOrigin : Internal\n ScriptStackTrace : at \u003cScriptBlock>, \u003cNo file>: line 1\n TargetSite :\n Name : Create\n DeclaringType : [System.Management.Automation.ScriptBlock]\n MemberType : Method\n Module : System.Management.Automation.dll\n Data : System.Collections.ListDictionaryInternal\n Source : System.Management.Automation\n HResult : -2146233087\n StackTrace :\n at System.Management.Automation.ScriptBlock.Create(Parser parser, String fileName, String fileContents)\n at System.Management.Automation.ExternalScriptInfo.ParseScriptContents(Parser parser, String fileName, String fileContents, Nullable`1 definingLanguageMode)\n at System.Management.Automation.ExternalScriptInfo.get_ScriptBlock()\n at System.Management.Automation.CommandDiscovery.CreateCommandProcessorForScript(ExternalScriptInfo scriptInfo, ExecutionContext context, Boolean useNewScope, SessionStateInternal sessionState)\n at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)\n at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)\n at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)\n at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)\n at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections,\nFunctionContext funcContext)\n at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\nCategoryInfo : ParserError: (:) [], ParseException\nFullyQualifiedErrorId : TypeNotFound\nInvocationInfo :\n ScriptLineNumber : 7\n OffsetInLine : 3\n HistoryId : 2\n ScriptName : ......\\ClassS1.ps1\n Line : [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n\n Statement : OBSWebsocketDotNet.OBSWebsocket\n PositionMessage : At ......\\ClassS1.ps1:7 char:3\n + [OBSWebsocketDotNet.OBSWebsocket] $obsWS = $null\n + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n PSScriptRoot : ......\\Classes\n PSCommandPath : ......\\ClassS1.ps1\n CommandOrigin : Internal\nScriptStackTrace : at \u003cScriptBlock>, \u003cNo file>: line 1\n```\n\n### Environment data\n\n```powershell\nName Value\n---- -----\nPSVersion 7.5.4\nPSEdition Core\nGitCommitId 7.5.4\nOS Microsoft Windows 10.0.19045\nPlatform Win32NT\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\nPSRemotingProtocolVersion 2.3\nSerializationVersion 1.1.0.1\nWSManStackVersion 3.0\n```\n\n### Visuals\n\n_No response_","author":{"url":"https://github.com/frogeater","@type":"Person","name":"frogeater"},"datePublished":"2025-11-18T15:56:23Z","url":"https://github.com/PowerShell/PowerShell/issues/26477"}
| 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:85dd01cc-ef7f-188e-a4c3-734f2e0f0a03 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E5B8:5C8B1:D5C94E:134E591:6AFDB119 |
| html-safe-nonce | 29f1fee6f6adfc437aded739b64710626eef12308469fa001d880e4a7acfce7f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNUI4OjVDOEIxOkQ1Qzk0RToxMzRFNTkxOjZBRkRCMTE5IiwidmlzaXRvcl9pZCI6IjY3NDkyMTc1Mzc1MDc4MzIwODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 31c1356a6aff76cf4b5dc0746fed06111c3de1630335072ee90bb0f0e3586886 |
| hovercard-subject-tag | issue:3638881594 |
| 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/26477/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9066832b413f47a08c6e51f60e373839083e857dfedbf1677b8f6abcad2c2d1f/PowerShell/PowerShell/issues/26477 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9066832b413f47a08c6e51f60e373839083e857dfedbf1677b8f6abcad2c2d1f/PowerShell/PowerShell/issues/26477 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | frogeater |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3c532f4d069d6fbe9b1837450343d1b9e0619a7d5c4ba0b3b64fd8e0bae67c7e |
| 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 | 5396ad00d1c6d19097aee7fc4d00a2eb03432c8f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width