Powershell 如何在运行时创建隐藏属性

Powershell 如何在运行时创建隐藏属性,powershell,class,properties,hidden,Powershell,Class,Properties,Hidden,我正在尝试创建一个具有只读属性的powershell类,每个类都有一个读取其隐藏对应项的getter(Property1>\u Property1) 这部分很好,但我无法隐藏属性1 我在下面包含了精简的代码-实际代码从数据库检索属性/值 有人知道如何在运行时隐藏属性吗 cls 类TestClass { TestClass() { #要创建的属性列表 #在运行时,这是从数据库表中检索的 $DBResult=New Object-TypeName PSObject-Property@{'Prope

我正在尝试创建一个具有只读属性的powershell类,每个类都有一个读取其隐藏对应项的getter(Property1>\u Property1) 这部分很好,但我无法隐藏属性1

我在下面包含了精简的代码-实际代码从数据库检索属性/值

有人知道如何在运行时隐藏属性吗

cls
类TestClass
{
TestClass()
{ 
#要创建的属性列表
#在运行时,这是从数据库表中检索的
$DBResult=New Object-TypeName PSObject-Property@{'Property1'='value1';'Property2'='value2';'Property3'='value3'}
#创建只读属性
foreach($DBResult | Get Member-MemberType Properties.Name中的Name)
{
$Getter=[ScriptBlock]::Create(“返回`$this.\u$Name”)
$Setter=[ScriptBlock]::Create(“写入警告”“$Name”是只读属性!”)
写入主机“Getter[$Getter]”-ForegroundColor黄色
写入主机“Setter[$Setter]”-前底色黄色
写入主机(“隐藏的[string]`${0}='{1}'-f$Name,$DBResult.$Name”)
#如何在运行时创建隐藏属性?
$this | Add Member-MemberType NoteProperty-Name“$Name”-Value$DBResult.“$Name”
#调用表达式(“隐藏的[string]`${0}='{1}'-f$Name,$DBResult.$Name”)
#创建可见的只读属性
$this | Add Member-MemberType ScriptProperty-Name$Name-Value$Getter-SecondValue$Setter
}
}
}
$Test=[TestClass]::new()
$Test | fl-财产*
也许是这个

$DBResults = New-Object -TypeName PSObject -Property @{'Property1' = 'value1' ; 'Property2' = 'value2' ; 'Property3' = 'value3'}

$ClassName = "TestClass"

$ClassDeclaration = "public class $ClassName"
$ClassDeclaration += [System.Environment]::NewLine
$ClassDeclaration += "{"
$TestString = ""

$PropertyList = @()

foreach ($Name in ($DBResults | Get-Member -MemberType Properties).Name)
{
    $Value = $DBResult."$Name"
    $ClassDeclaration += $([System.Environment]::NewLine)
    $ClassDeclaration += "private string @$("_" + $Name) = `"$Value`";"
    $ClassDeclaration += $([System.Environment]::NewLine)
    $ClassDeclaration += "public string @$("get" + $Name) { get { return $("_" + $Name); } }"
}

$ClassDeclaration += $([System.Environment]::NewLine)
$ClassDeclaration += "}"

$FinalClassDefinition = @"
    $ClassDeclaration
"@

try {Add-Type -Language CSharp -TypeDefinition $FinalClassDefinition -IgnoreWarnings} catch { return $_.Exception.Message }

$Test = [TestClass]::new()
$Test  | fl -Property *
解析似乎不起作用,但代码很好;-)

阿瓦隆的荣誉77

您的代码是面向csharp的,不太适合我的项目,但它让我想到用字符串构建类并将其提供给调用表达式。 它不是干净的,但在powershell允许添加隐藏成员(添加成员)之前,可以执行以下操作:

cls
$DBResult=New Object-TypeName PSObject-Property@{'Property1'='value1';'Property2'='value2';'Property3'='value3'}
调用表达式-命令@”
类TestClass
{
#创建隐藏属性
$($DBResult | Get Member-MemberType属性| ForEach对象{“`thidden`${0}='{1}`n”-f$\\.Name,$DBResult.$($\.Name)“})
TestClass()
{ 
#创建可见的只读属性
$($DBResult |获取成员-成员类型属性| ForEach对象{
“`t`t`$this | Add Member-MemberType ScriptProperty-Name'{0}'-Value{1}返回`this.{0}{2}-SecondValue{1}Write Warning'[{0}]是一个只读属性!'{2}`n“-f${.Name,{,}”
}
)
}
}
"@
$Test=[TestClass]::new()
$Test | fl-财产*
$Test.Property1='尝试更改只读值'
$Test.\u Property1='changed value'
$Test | fl-财产*
此字符串生成的结果是:

Class测试类
{
#创建隐藏属性
隐藏的$\u属性1='value1'
隐藏的$\u属性2='value2'
隐藏的$\u属性3='value3'
TestClass()
{ 
#创建可见的只读属性
$this | Add Member-MemberType ScriptProperty-Name'Property1'-Value{返回$this。_Property1}-SecondValue{Write Warning'[Property1]是只读属性!'}
$this | Add Member-MemberType ScriptProperty-Name'Property2'-Value{返回$this。_Property2}-SecondValue{Write Warning'[Property2]是只读属性!'}
$this | Add Member-MemberType ScriptProperty-Name'Property3'-Value{返回$this。_Property3}-SecondValue{Write Warning'[Property3]是只读属性!'}
}
}

甚至
隐藏的
属性都不是私有的。当使用
Get Member-Force
时,它们是可见的,这没有问题。程序员有责任不乱动隐藏的属性。只要他们没有在没有使用武力的情况下被列入名单