zoukankan      html  css  js  c++  java
  • PowerShell笔记

    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
    ```PowerShell
    class Point
    {
        ## 使用PowerShell正常的变量语法来定义两个属性,
        ## 你也可以限制变量的属性。
        ## 使用正常的类型限制符:
        ## [type] $VarName = initialValue
        $X = 0
        $Y = 0
        ## 定义一个方法(返回值为Void)
        ## 定义两个参数,
        ## 当然你可以给参数添加类型
        [void] Move($xOffset, $yOffset)
        {
            $this.X += $xOffset
            $this.Y += $yOffset
        }
    }
    ## 创建一个Point 类型,并调用方法
    $point = [Point]::new()
    $point.Move(10, 20)
    $point | Get-Member *
    
    
  • 相关阅读:
    「专题总结」后缀自动机
    「专题总结」回文自动机
    「专题总结」后缀数组
    2.11毕设进度
    2.10毕设进度
    2.09毕设进度
    2.08毕设进度
    2.07毕设进度
    2.06毕设进度
    2.05毕设进度
  • 原文地址:https://www.cnblogs.com/MerLin-LiuNian/p/15262726.html
Copyright © 2011-2022 走看看