1. 程式人生 > 其它 >PowerShell筆記 - 5.物件、屬性、方法

PowerShell筆記 - 5.物件、屬性、方法

5.物件

本系列是一個重新學習PowerShell的筆記,內容引用自PowerShell中文部落格

基本操作

新增屬性及操作

#AliasProperty:另外一個屬性的別名
#CodeProperty:通過靜態的.Net方法返回屬性的內容
#Property:真正的屬性
#NoteProperty:隨後增加的屬性
#ScriptProperty:通過指令碼執行返回一個屬性的值
#ParameterizedProperty:需要傳遞引數的屬性
#CodeMethod:對映到靜態的.NET方法
#Method:正常的方法
#ScriptMethod:一個執行Powershell指令碼的方法

PS C:> $obj = New-Object System.Object                                                                       PS C:> Add-Member -InputObject $obj -MemberType NoteProperty -Name Color -Value "Red"                        PS C:> $obj | Add-Member -MemberType NoteProperty Width "20px"                                               PS C:> Add-Member -InputObject $obj -MemberType ScriptMethod -Name Show -Value {"這是一個動作"}
PS C:> $obj.Show()                                                                                         

這是一個動作

PS C:> $obj                                                                                                  
Color Width
----- -----
Red   20px


PS C:> $obj | Get-Member                                                                                     

   TypeName: System.Object

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Color       NoteProperty string Color=Red
Width       NoteProperty string Width=20px
Show        ScriptMethod System.Object Show();

載入程式集物件

建立Test.dll

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Test
{
    public class Student
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public Student(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public override string  ToString()
        {
            return string.Format("Name={0};Age={1}", this.Name,this.Age);
        }
    }
}

載入Dll並建立物件

PS C> ls .Test.dll

    目錄: C

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2012/1/13     10:49       4608 Test.dll

PS C> $TestDLL=ls .Test.dll
PS C> [reflection.assembly]::LoadFile($TestDLL.FullName)

GAC    Version        Location
---    -------        --------
False  v2.0.50727     CTest.dll

PS C> $stu=New-Object Test.Student('Mosser',22)
PS C> $stu

Name                                                                        Age
----                                                                        ---
Mosser                                                                       22

PS C> $stu.ToString()
Name=Mosser;Age=22

定義類

class Point
{
    ## 使用PowerShell正常的變數語法來定義兩個屬性,
    ## 你也可以限制變數的屬性。
    ## 使用正常的型別限制符:
    ## [type] $VarName = initialValue
    $X = 0
    $Y = 0
    ## 定義一個方法(返回值為Void)
    ## 定義兩個引數,
    ## 當然你可以給引數新增型別
    [void] Move($xOffset, $yOffset)
    {
        $X += $xOffset
        $Y += $yOffset
    }
}
## 建立一個Point 型別,並呼叫方法
$point = [Point]::new()
$point.Move(10, 20)
$point