PowerShell类构造函数-获取内容返回字符串而不是数组
我有一个文本文件,其中有一个测试设备列表,如下所示PowerShell类构造函数-获取内容返回字符串而不是数组,powershell,class,constructor,Powershell,Class,Constructor,我有一个文本文件,其中有一个测试设备列表,如下所示 D1 D2 D3 我有一个类构造函数,它从文本文件中获取内容并将其分配给属性 如果在构造函数中运行$this.TestList=Get Content。\TestList.txt,则会得到一个字符串D1 D2 D3 如果在构造函数中运行$TestList=Get Content.\TestList.txt,则会得到一个数组 如果我运行$TestList=Get Content。\TestList.txt后跟$this.TestList=$Te
D1
D2
D3
我有一个类构造函数,它从文本文件中获取内容并将其分配给属性
如果在构造函数中运行$this.TestList=Get Content。\TestList.txt,则会得到一个字符串D1 D2 D3
如果在构造函数中运行$TestList=Get Content.\TestList.txt,则会得到一个数组
如果我运行$TestList=Get Content。\TestList.txt后跟$this.TestList=$TestList,我会得到一个字符串
我不熟悉PowerShell中的类,所以这可能是一个简单的代码错误,但我不知道为什么会发生这种情况。有人能解释一下吗
编辑:
这是一个非常精简的代码版本
class CMBuildInfo {
[string]$TestMachine
CMBuildInfo () {
$this.Testmachine = Get-Content "C:\test\ProTestmachines.txt"
}
}
[CMBuildInfo]$NewApplication = New-Object -TypeName CMBuildInfo -ArgumentList($LatestUpdateInfo)
$NewApplication.TestMachine
我已经确定了这个问题,它是类中TestMachine的转换。最初它是一个简单的字符串。当我决定切换到一个数组时,我在TestMachine下面的属性声明上加了额外的方括号
有趣的是意外的结果。将Get内容强制转换为字符串,它似乎用一个空格清除器将每一行连接在一起
正确的代码是:
class CMBuildInfo {
[string[]]$TestMachine
CMBuildInfo () {
$this.Testmachine = Get-Content "C:\test\ProTestmachines.txt"
}
}
[CMBuildInfo]$NewApplication = New-Object -TypeName CMBuildInfo -ArgumentList($LatestUpdateInfo)
$NewApplication.TestMachine
继续我的评论,从文档和帮助文件推断 请理解$this仅在类的当前实例中使用。 $this仅在方法体的上下文中有效。 在类主体之外,$这意味着什么都没有,它将通过设计返回null。没有点引用,您无法为其分配任何内容。 这是类中要在类实例中使用的值的引用,该值将由传递的变量内容保存。 这些,$this.TestList=获取内容。\TestList.txt和 $TestList=获取内容。\TestList.txt后跟$this.TestList=$TestList对于构造函数无效;如第3点所述。 PowerShell将动态播放,除非您另有说明,如我在评论中所述。然而,这实际上是针对1:1的内容,而不是文件的内容 一个简单的类、构造函数和用例如下:
class New_Car {
# descriptors/properties/state
[string]$Color
[string]$Name
[string]$Manufacturer
[string]$Model
[int]$Length
[int]$Width
[int]$Height
[int]$Mileage
# Methods, to perform on the object
[void]Drive($Milage)
{
<#
Reference for a current instance of the object
and is only valid in this method body
#>
$this.Mileage += $Mileage
}
}
([New_Car]).GetType() |
Format-Table -AutoSize
# Results
<#
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
False True RuntimeType System.Reflection.TypeInfo
#>
# Instantiate the constructor
($MyNewCar = [New_Car]::new())
# Results
<#
Color :
Name :
Manufacturer :
Model :
Length : 0
Width : 0
Height : 0
Mileage : 0
#>
($MyNewCar).GetType() |
Format-Table -AutoSize
# Results
<#
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False New_Car System.Object
#>
$MyNewCar | Get-Member
# Results
<#
TypeName: New_Car
Name MemberType Definition
---- ---------- ----------
Drive Method void Drive(System.Object Milage)
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Color Property string Color {get;set;}
Height Property int Height {get;set;}
Length Property int Length {get;set;}
Manufacturer Property string Manufacturer {get;set;}
Mileage Property int Mileage {get;set;}
Model Property string Model {get;set;}
Name Property string Name {get;set;}
Width Property int Width {get;set;}
#>
$MyNewCar.Drive(9)
$MyNewCar
# Results
<#
Color :
Name :
Manufacturer :
Model :
Length : 0
Width : 0
Height : 0
Mileage : 9
#>
$MyNewCar.Manufacturer = Get-Content -Path 'D:\Temp\CarMakers.txt'
$MyNewCar
# Results
<#
Color :
Name :
Manufacturer : BMW GM Ford
Model :
Length : 0
Width : 0
Height : 0
Mileage : 9
#>
$CarFolks = Get-Content -Path 'D:\Temp\CarMakers.txt'
$MyNewCar.Manufacturer = $CarFolks
$MyNewCar
# Results
<#
Color :
Name :
Manufacturer : BMW GM Ford
Model :
Length : 0
Width : 0
Height : 0
Mileage : 9
#>
…不会产生智能感知,不会产生任何结果,作为保留字/功能,不能/不应用于其他目的
最后,如果您试图在一个类中加载多个对象,那么您将看到构造函数重载。所以,这个
Class Car
{
[String]$vin
static [int]$numberOfWheels = 4
[int]$numberOfDoors
[datetime]$year
[String]$model
Car ([string]$vin)
{$this.vin = $vin }
Car ([string]$vin, [string]$model)
{
$this.vin = $vin
$this.model = $model
}
Car ([string]$vin, [string]$model, [datetime]$year)
{
$this.vin = $vin
$this.model = $model
$this.year = $year
}
Car ([string]$vin, [string]$model, [datetime]$year, [int]$numberOfDoors)
{
$this.vin = $vin
$this.model = $model
$this.year = $year
$this.numberOfDoors = $numberOfDoors
}
}
[car]::new
# Results
<#
OverloadDefinitions
-------------------
Car new(string vin)
Car new(string vin, string model)
Car new(string vin, string model, datetime year)
Car new(string vin, string model, datetime year, int numberOfDoors)
#>
[car]::new(1234, 'chevy', '1/2/2015', 3)
# Results
<#
vin numberOfDoors year model
--- ------------- ---- -----
1234 3 02-Jan-15 00:00:00 chevy
#>
请参阅此处的详细信息:SO规则:-在PowerShell v5及更高版本上有许多可靠的文章和Youtube视频?Youtube关于PowerShell类您需要显示类的简化版本,以及您在此处显示的内容。然而,你所做的是无效的。请参阅指向的链接。总之,这不是一个Get-Content cmdlet问题。这就是你如何使用它。一般来说,尽管有这些类,PowerShell还是会根据您所传递的内容尝试提供帮助。这意味着它将根据输入或强制转换分配,从一种类型重铸到另一种类型,如果您没有明确地以一种或另一种方式进行强制转换,这要感谢所有响应。在回答所有关于如何发布的提示时,我认为这是一个简单的查询,如果我发布了MRE,它就会是;如果我再发一次,我会更清楚。我花了好几个小时在网上寻找答案,正如你从识别出的故障中所看到的那样,我不太可能在网上找到解决方案。这只是一个愚蠢的错误,我看不出来,因为PS中的类对我来说是新的,这让我的头脑蒙上了一层阴影@非常感谢您的周到努力。
Class Car
{
[String]$vin
static [int]$numberOfWheels = 4
[int]$numberOfDoors
[datetime]$year
[String]$model
Car ([string]$vin)
{$this.vin = $vin }
Car ([string]$vin, [string]$model)
{
$this.vin = $vin
$this.model = $model
}
Car ([string]$vin, [string]$model, [datetime]$year)
{
$this.vin = $vin
$this.model = $model
$this.year = $year
}
Car ([string]$vin, [string]$model, [datetime]$year, [int]$numberOfDoors)
{
$this.vin = $vin
$this.model = $model
$this.year = $year
$this.numberOfDoors = $numberOfDoors
}
}
[car]::new
# Results
<#
OverloadDefinitions
-------------------
Car new(string vin)
Car new(string vin, string model)
Car new(string vin, string model, datetime year)
Car new(string vin, string model, datetime year, int numberOfDoors)
#>
[car]::new(1234, 'chevy', '1/2/2015', 3)
# Results
<#
vin numberOfDoors year model
--- ------------- ---- -----
1234 3 02-Jan-15 00:00:00 chevy
#>