Members of a class in embedded C# code cannot be parsed

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
davaleng
Posts: 6
Last visit: Wed Aug 10, 2022 10:36 pm

Members of a class in embedded C# code cannot be parsed

Post by davaleng »

Hello,

I have a problem,

Powershell studio cannot parse class members in embedded C# code

I often need to embed C# code in PS to implement some functionality, parsing members of C# classes, which is a very useful feature

I tested two other software, powershell IES and PowerGUI, Class members in C# can be parsed perfectly, Take a look at the image demonstration below

Another serious problem is that the code not have any output after execution :oops:


Powershell studio v5.8.209
system windows7 64bit
powershell 5.1
abc.gif
abc.gif (433.05 KiB) Viewed 2640 times
Powershell code:

Code: Select all

$code = @'
namespace ClassLibrary1
{
	public class Class1
	{
		public static void SayHello()
		{
			System.Console.WriteLine("hello world");
		}
	}
}
'@

Add-Type -TypeDefinition $code

[ClassLibrary1.Class1]::SayHello()
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: Members of a class in embedded C# code cannot be parsed

Post by Alexander Riedel »

I am not sure what the project team will have to say to this, but your construct will never work for intellisense in PowerShell Studio.
Generally, you would embed C# code like described here: https://dandraka.com/2018/11/12/powersh ... -inline-c/
I have not tried this with PowerShell Studio and I do not know if THAT would work, I will leave that to the team.

Your construct separates the code in the here string and uses a variable as an argument to Add-Type.
In order for that to be resolved, the script MUST run. Any use of a variable in that scenario can ONLY be resolved at runtime,
as the variable has no content unless you execute the code.

The ISE can only do that when you run that script first. Not when you just load it.
The requirement to actually run a script to get intellisense is quite dangerous and a security risk. You should not have to do that and the ISE trains users to just run a script to see intellisense.
That leads to users running scripts without considering security implications. We will not ever require you to run code just to see members. It is too dangerous and can be misleading.
We have seen editors that run PowerShell code in the background WITHOUT user interaction to provide better intellisense. It sure looks like magic but is an immense security risk.
Just imagine you get that malware script emailed, you want to see what makes it tick and your editor just runs its code internally to provide intellisense. Its a nightmare.

The second part to this is that by running code and not clearing the runspace between subsequent script executions, you get a contaminated runspace.
See here: https://www.sapien.com/blog/2022/04/25/ ... -avoid-it/
Your different output is quite likely the result of runspace contamination.
It is incredibly difficult to get reproducible results in the ISE because of that. I am not saying that the ISE is bad, it just was not designed for script development really.
In your example load the script and run it in the ISE.
Now rename ClassLibrary1 to MyClassLibrary and Class1 to MyClass
but leave this line intact:
[ClassLibrary1.Class1]::SayHello()

Run it. Your output is the same, it works fine, even though Class1 no longer even exits in your code.
Typing
[ClassLibrary1.Class1]::
still gives you intellisense for something that is no longer there.
It will work all day and night until you restart the ISE, making you think your code works fine. But it'll fail any place else.
This is perfectly fine for some 15 line scripts, where you can easily see and understand what is going on. If your script gets into the hundreds or thousands of lines while dot sourcing code and using modules, this is not an easy situation.

I hope this explanation helps to illustrate this effect.
Alexander Riedel
SAPIEN Technologies, Inc.
davaleng
Posts: 6
Last visit: Wed Aug 10, 2022 10:36 pm

Re: Members of a class in embedded C# code cannot be parsed

Post by davaleng »

Thank you for your reply

Your point of view, I agree

But embedded C# code is generally not very many lines, It's still convenient to add smart hints for members

I tried powershell ise , powerGUI ,Visual Studio Code,And some other software, they are very good at smart tips for Classses members
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: Members of a class in embedded C# code cannot be parsed

Post by Alexander Riedel »

As I said there may be a way to construct the code so this can be done without running it. But that I will need to discuss with the development team.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.