A
A
Alexey2019-04-21 21:41:11
PowerShell
Alexey, 2019-04-21 21:41:11

How to access system classes from user class methods?

From a regular function, you can normally refer, for example, to System.Windows.Forms.Cursor

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

function getPos(){
    [System.Windows.Forms.Cursor]::Position
}

getPos

IsEmpty    X   Y
-------    -   -
  False 1001 460

If you create a class, then inside the method there is no longer access to System.Windows.Forms.Cursor
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

class A{
    static getPos(){
        [System.Windows.Forms.Cursor]::Position
    }
}

[A]::getPos

+         [System.Windows.Forms.Cursor]::Position
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
Не удалось найти тип [System.Windows.Forms.Cursor].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound

Tell me how to be?
PS Version 5.1.17763.316

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-04-22
@TsSaltan

I don’t follow that they messed up in five, maybe just

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Add-Type -TypeDefinition @'

using System.Windows.Forms;
using  System.Drawing;
public class A{
    public static Point getPos(){
       return Cursor.Position;
    }
}
'@  -ReferencedAssemblies 'System.Windows.Forms.dll','System.Drawing.dll'
[A]::getPos()

IsEmpty    X   Y
-------    -   -
  False 1112 316

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question