以编程方式在 Class Ctor 中设置 Public 属性
Programmatically Setting Public Property in Class Ctor
我想在 PowerShell V5.0 的 class' 构造函数中以编程方式设置 public 属性。测试 class 有许多 public 属性,根据传递给 class' 构造函数的对象(亲切地称为“$something”)
我认为如果我创建一个可访问的 public 属性 名称数组并遍历它们,作为设置 public properties 只是在传递给它的 $something 对象上调用相同的方法。
尝试在每个范围内使用 Set-Variable cmdlet(我认为它是本地范围)。可以在构造函数中手动设置每个 public 属性,但我希望能够尽可能缩短它。
Class TestClass
{
[STRING]$PublicProperty
[STRING]$PublicProperty1
... ... ... #Loads more properties
[STRING]$PublicProperty50
#An array that contains name for public properties
[ARRAY]$ArrayOfPublicProperties = @("PublicProperty", "PublicProperty1"...)
TestClass($something)
{
foreach ($property in $this.ArrayOfPublicProperties)
{
Set-Variable -Name "$($property.Name)" -Scope Local -Value $($something.GetValue(#blablabla))
}
}
}
我希望能够遍历 public 数组(这是粗略的代码,但它适用于我所处的情况,但我不知道其他任何方法)和使用 Set-Variable cmdlet 设置变量。相反,它没有设置任何东西。我确定我过去做过类似的事情,以编程方式创建和设置变量等...... idk。
请帮忙
Set-Variable
仅适用于常规变量,不适用于属性。
你必须通过.psobject.properties
使用反射,这也避免了对$ArrayOfPublicProperties
助手属性:
的需要
Class TestClass
{
[string]$PublicProperty
[string]$PublicProperty1
# ...
TestClass($something)
{
# Loop over all properties of this class.
foreach ($prop in $this.psobject.Properties)
{
$prop.Value = $something.GetValue(#blablabla)
}
}
}
但是请注意,PowerShell 方便地允许通过 cast 从 hashtable[=71= 构造和初始化对象] 或具有 匹配属性 .
的 (自定义)对象
注意事项:要让它起作用:
- class 必须有 NO 构造函数(即隐式仅支持无参数默认构造函数),
或,如果有个构造函数:
- 一个无参数构造函数必须存在。
- 症状,如果不满足该条件:类型转换错误:
Cannot convert ... to type ...
- AND 也没有 单参数构造函数(隐式)键入
[object]
或 [hashtable]
(使用 hashtable 转换参数,[psobject]
/ [pscustomobject]
很好,但是)。
- 症状,如果不满足该条件:调用单参数构造函数。
输入哈希表/对象的 属性 名称集必须是目标 class 属性的 子集;换句话说:输入对象不得包含目标 class 中不存在的属性,但不能包含 all target-class属性必须存在.
应用于您的示例(请注意,不再有显式构造函数,因为原始构造函数 TestClass($something)
会破坏该功能,因为 $something
是隐式的 [object]
-输入):
Class TestClass
{
[string]$PublicProperty
[string]$PublicProperty1
# ...
# Note: NO (explicit) constructor is defined.
}
# Construct a [TestClass] instance and initialize its properties
# from a hashtable, using a cast.
[TestClass] @{ PublicProperty = 'p0'; PublicProperty1 = 'p1'}
我想在 PowerShell V5.0 的 class' 构造函数中以编程方式设置 public 属性。测试 class 有许多 public 属性,根据传递给 class' 构造函数的对象(亲切地称为“$something”)
我认为如果我创建一个可访问的 public 属性 名称数组并遍历它们,作为设置 public properties 只是在传递给它的 $something 对象上调用相同的方法。
尝试在每个范围内使用 Set-Variable cmdlet(我认为它是本地范围)。可以在构造函数中手动设置每个 public 属性,但我希望能够尽可能缩短它。
Class TestClass
{
[STRING]$PublicProperty
[STRING]$PublicProperty1
... ... ... #Loads more properties
[STRING]$PublicProperty50
#An array that contains name for public properties
[ARRAY]$ArrayOfPublicProperties = @("PublicProperty", "PublicProperty1"...)
TestClass($something)
{
foreach ($property in $this.ArrayOfPublicProperties)
{
Set-Variable -Name "$($property.Name)" -Scope Local -Value $($something.GetValue(#blablabla))
}
}
}
我希望能够遍历 public 数组(这是粗略的代码,但它适用于我所处的情况,但我不知道其他任何方法)和使用 Set-Variable cmdlet 设置变量。相反,它没有设置任何东西。我确定我过去做过类似的事情,以编程方式创建和设置变量等...... idk。
请帮忙
Set-Variable
仅适用于常规变量,不适用于属性。
你必须通过.psobject.properties
使用反射,这也避免了对$ArrayOfPublicProperties
助手属性:
Class TestClass
{
[string]$PublicProperty
[string]$PublicProperty1
# ...
TestClass($something)
{
# Loop over all properties of this class.
foreach ($prop in $this.psobject.Properties)
{
$prop.Value = $something.GetValue(#blablabla)
}
}
}
但是请注意,PowerShell 方便地允许通过 cast 从 hashtable[=71= 构造和初始化对象] 或具有 匹配属性 .
的 (自定义)对象注意事项:要让它起作用:
- class 必须有 NO 构造函数(即隐式仅支持无参数默认构造函数),
或,如果有个构造函数:
- 一个无参数构造函数必须存在。
- 症状,如果不满足该条件:类型转换错误:
Cannot convert ... to type ...
- 症状,如果不满足该条件:类型转换错误:
- AND 也没有 单参数构造函数(隐式)键入
[object]
或[hashtable]
(使用 hashtable 转换参数,[psobject]
/[pscustomobject]
很好,但是)。- 症状,如果不满足该条件:调用单参数构造函数。
- 一个无参数构造函数必须存在。
输入哈希表/对象的 属性 名称集必须是目标 class 属性的 子集;换句话说:输入对象不得包含目标 class 中不存在的属性,但不能包含 all target-class属性必须存在.
应用于您的示例(请注意,不再有显式构造函数,因为原始构造函数 TestClass($something)
会破坏该功能,因为 $something
是隐式的 [object]
-输入):
Class TestClass
{
[string]$PublicProperty
[string]$PublicProperty1
# ...
# Note: NO (explicit) constructor is defined.
}
# Construct a [TestClass] instance and initialize its properties
# from a hashtable, using a cast.
[TestClass] @{ PublicProperty = 'p0'; PublicProperty1 = 'p1'}