国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PowerShell > 正文

Windows Powershell創建對象

2020-05-30 20:16:47
字體:
來源:轉載
供稿:網友

通過New-Object創建新對象

如果使用構造函數創建一個指定類型的實例對象,該類型必須至少包含一個簽名相匹配的構造函數。例如可以通過字符和數字創建一個包含指定個數字符的字符串:

代碼如下:
PS C:Powershell> New-Object String(‘*',100)

*******************************************************************************
*********************

為什么支持上面的方法,原因是String類中包含一個Void .ctor(Char, Int32) 構造函數

代碼如下:
PS C:Powershell> [String].GetConstructors() | foreach {$_.tostring()}
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char[])
Void .ctor(Char, Int32)

通過類型轉換創建對象

通過類型轉換可以替代New-Object

代碼如下:
PS C:Powershell> $date="1999-9-1 10:23:44"
PS C:Powershell> $date.GetType().fullName
System.String
PS C:Powershell> $date
1999-9-1 10:23:44
PS C:Powershell> [DateTime]$date="1999-9-1 10:23:44"
PS C:Powershell> $date.GetType().FullName
System.DateTime
PS C:Powershell> $date

1999年9月1日 10:23:44

如果條件允許,也可以直接將對象轉換成數組

代碼如下:
PS C:Powershell> [char[]]"mossfly.com"
m
o
s
s
f
l
y
.
c
o
m
PS C:Powershell> [int[]][char[]]"mossfly.com"
109
111
115
115
102
108
121
46
99
111
109

加載程序集

自定義一個簡單的C#類庫編譯為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);
        }
    }
}

在Powershell中加載這個dll并使用其中的Student類的構造函數生成一個實例,最后調用ToString()方法。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥滨县| 新竹市| 漯河市| 柘城县| 商丘市| 曲阳县| 筠连县| 赣榆县| 屯门区| 泰顺县| 建阳市| 康定县| 鄯善县| 揭阳市| 益阳市| 洞头县| 温泉县| 拉孜县| 武城县| 武邑县| 志丹县| 翁源县| 湘西| 五寨县| 北流市| 社会| 长沙县| 酉阳| 泸溪县| 东阿县| 伊川县| 赣榆县| 虞城县| 延吉市| 基隆市| 根河市| 巴林右旗| 招远市| 灵璧县| 醴陵市| 汝阳县|