You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using external types / classes in Custom PowerShell Classes (v5) results in Parsing Error #2074
Issue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeWG-Languageparser, language semantics
I am trying to create a PowerShell-Project with my own custom PowerShell-Classes.
I use explicit type defintion for all attributes of my custom classes. For all integrated .Net-Classes there is now problem, but for other classes (for example external dll) it does not work.
It seems like, that you have to create a Wrapper script which loads the dll / Imports the module and afterwards starts the actual script project (with custom classes) by dotsourcing.
Unfortunately this workaround (Wrapper-Script) does not work out in my scenario --> A automation / orchestration engine only accepts one script...
So is there a way to fix this? The Parser should not throw an error, as you declare (using statement) that you will use classes out of this namespace. If there is something wrong with the class itself.. .this should be a runtime exception, or am I wrong?
Steps to reproduce
usingnamespaceMicrosft.ActiveDirectory.Management#These two do not work, too #using module ActiveDirectory#Import-Module ActiveDirectoryclassTest
{
[Microsoft.ActiveDirectory.Management.ADDomain] $domain;
}
...
Expected behavior
no Error Message
Actual behavior
At line:9 char:6+ [Microsoft.ActiveDirectory.Management.ADDomain] $domain;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.ActiveDirectory.Management.ADDomain].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
In this case with an integrated .NET class it perfectly works.
Rob Holt (@rjmholt) correct me if I'm wrong, but my understanding is that in order for this to work then at least the following would need to be done:
Classes would need to be defined with MetadataBuilder instead of the System.Reflection.Emit API's
Not yet loaded assemblies specified by using assembly would need to be analyzed via a MetadataReaderif classes are defined in the file
Has there been any discussion on if this would even be accepted as a contribution? As you know, code that utilizes those API's would be exponentially more complex than the current code.
I'd like to take on at some point if no one beats me to it, but I'm unsure if it would be considered worth maintaining.
Patrick Meinecke (@SeeminglyScience) - Using MetadataReader was my original plan when I introduced using assembly - I even wanted to use it for command analysis to avoid loading assemblies.
I can't comment on MetadataBuilder. I thought Reflection.Emit was up to the task, but I could easily be mistaken. One important aspect of defining types though - it should ideally be done in a collectible assembly.
Using MetadataReader was my original plan when I introduced using assembly - I even wanted to use it for command analysis to avoid loading assemblies.
Yes! I've been on and off working on a metadata only version of CmdletInfo for that 🙂
I can't comment on MetadataBuilder. I thought Reflection.Emit was up to the task, but I could easily be mistaken.
It is as long as we can load the assembly prior to actually defining the type which should be doable. Now that I think about it, I think I started down that path because I wanted the exceptions TypeBuilder.CreateType() throws, but at parse time. MetadataBuilder doesn't really do any validation though, so any of that logic would need to be written from scratch utilizing MetadataReader anyway.
One important aspect of defining types though - it should ideally be done in a collectible assembly.
Ah yeah, MetadataBuilder is out then. I thought they weren't collectible for some reason.
Should be a lot less complicated with just MetadataReader then. Thanks Jason Shirk (@lzybkr)!
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Issue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeWG-Languageparser, language semantics
Activity
SteveL-MSFT commentedon Oct 28, 2016
I believe we implemented 'using assembly' specifically for this use case
joeyaiello commentedon Feb 16, 2017
According to Jason Shirk (@lzybkr),
using assemblyis meant for this, but it has not been implemented for this use case yet.12 remaining items
SeeminglyScience commentedon May 22, 2019
Rob Holt (@rjmholt) correct me if I'm wrong, but my understanding is that in order for this to work then at least the following would need to be done:
MetadataBuilderinstead of theSystem.Reflection.EmitAPI'susing assemblywould need to be analyzed via aMetadataReaderif classes are defined in the fileHas there been any discussion on if this would even be accepted as a contribution? As you know, code that utilizes those API's would be exponentially more complex than the current code.
I'd like to take on at some point if no one beats me to it, but I'm unsure if it would be considered worth maintaining.
lzybkr commentedon May 22, 2019
Patrick Meinecke (@SeeminglyScience) - Using
MetadataReaderwas my original plan when I introducedusing assembly- I even wanted to use it for command analysis to avoid loading assemblies.I can't comment on
MetadataBuilder. I thoughtReflection.Emitwas up to the task, but I could easily be mistaken. One important aspect of defining types though - it should ideally be done in a collectible assembly.SeeminglyScience commentedon May 22, 2019
Yes! I've been on and off working on a metadata only version of
CmdletInfofor that 🙂It is as long as we can load the assembly prior to actually defining the type which should be doable. Now that I think about it, I think I started down that path because I wanted the exceptions
TypeBuilder.CreateType()throws, but at parse time.MetadataBuilderdoesn't really do any validation though, so any of that logic would need to be written from scratch utilizingMetadataReaderanyway.Ah yeah,
MetadataBuilderis out then. I thought they weren't collectible for some reason.Should be a lot less complicated with just
MetadataReaderthen. Thanks Jason Shirk (@lzybkr)!rjmholt commentedon Jan 29, 2020
Just while I'm compiling some information in this area on this...
Here's where
using assemblyis acted on:PowerShell/src/System.Management.Automation/engine/parser/Compiler.cs
Line 2701 in 7d5c869
Here's where that
LoadAssembly()call goes:PowerShell/src/System.Management.Automation/engine/parser/Compiler.cs
Lines 2761 to 2799 in 7d5c869
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.