0

The following code runs fine when executed in ISE:

class MyWindow : Windows.Window {
}
# for testing purposes this is the whole script

But when run without ISE it results in an error:

At C:\Users\Thelonius\git\ps-scripts\minimal.ps1:1 char:18
+ class MyWindow : Windows.Window {
+                  ~~~~~~~~~~~~~~
Unable to find type [Windows.Window].
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TypeNotFound

When I run it in a new Powershell window by doing .\minimal.ps1 the error is the same. After executing Add-Type -AssemblyName PresentationFramework in the shell .\minimal.ps1 runs without error. Since the error occurs while parsing the script adding the Add-Type command to the script is no option. As I understand it instead using assembly PresentationFramework should do the job. But it does not (neither in 5.0 nor in 5.1).

So is there a sane way to make it work without loading the assembly by Powershell command-line options or something similarly ugly?

TNT
  • 3,392
  • 1
  • 24
  • 27
  • 1
    Seems @PetSerAl is right if there is really no way to do it in a single file. Even using an `.lnk` file with something like `powershell.exe -c "Add-Type -AssemblyName PresentationFramework; & .\minimal.ps1"` in it seems way more elegant to me than messing with multiple files. – TNT Jun 03 '18 at 11:42
  • 1
    This is the entire point of `Add-Type`. Why doesn't it work for you exactly? – Maximilian Burszley Jun 03 '18 at 12:58
  • @TheIncorrigible1: Because `Add-Type` is executed at _runtime_, which is too late for the `class` definition to see the type, because it is processed earlier, at _parse time_ – mklement0 Jul 10 '18 at 00:14
  • @TNT: Indeed, `using assembly` _should_ do the job, but _doesn't yet_ - see https://stackoverflow.com/a/51255643/45375 – mklement0 Jul 10 '18 at 02:05
  • @mklement0 classes are hoisted? – Maximilian Burszley Jul 10 '18 at 03:56
  • 1
    @TheIncorrigible1: You can conceive of it that way, yes: unlike functions, classes have already been parsed by the time execution starts; try `[Foo]::new().Bar(); class Foo { [string] Bar() { return 'hi' } }` – mklement0 Jul 10 '18 at 04:17

1 Answers1

1

Use Add-Type first in your console:

To get The Assembly path first from ISE:

[System.Windows.Window].Assembly

Then use Add-Type with the location path in the console:

In my System is:

Add-Type -Path 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll'

Then it will work

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • This is good information, But, can you suggest a way to get this information from another user's computer? – lit Jun 03 '18 at 11:29
  • 1
    I don't get what the advantage is. The error occurs before `Add-Type` is executed. – TNT Jun 03 '18 at 11:44
  • 1
    @TNT then add it before, at the start of the script file – Avshalom Jun 03 '18 at 13:23
  • 1
    I'd suggest just putting `Add-Type -AssemblyName PresentationFramework` in his script before trying to inherit from it. You don't need to do all that extra work of finding where the assembly lives. – Maximilian Burszley Jun 03 '18 at 16:31
  • 1
    @avshalom the problem is that the class has to be defined before the Add-Type. I've ran into this problem in the past and the only solutions I've found is use multiple script files (or create the classes using reflection) – bluuf Jun 03 '18 at 17:07