PowerShell:什么是System.IO.StreamWriter和System.Diagnostics.ProcessStartInfo?

空值

我正在学习PowerShell。使用System.IO.StreamWriterSystem.Diagnostics.ProcessStartInfo,将它们称为“类”是否更准确?还是也可以将它们称为“新对象”?

$foo = New-Object System.Diagnostics.ProcessStartInfo
Mathias R. Jessen

System.IO.StreamWriterSystem.Diagnostics.ProcessStartInfo,以及更广泛类型(或数据类型)的.NET

PowerShell在.NET上运行,在.NET的类型系统中,“类”是我们称为引用类型对象的行为的蓝图

$foo = New-Object System.Diagnostics.ProcessStartInfo

在上面的语句中,我们要求New-Object创建一个实例ProcessStartInfo-也就是说,我们希望有一个对象,它的行为规定由ProcessStartInfo类。我们也可以说,结果对象(现在由引用$foo的类型为ProcessStartInfo

引用类型与值类型

上面我提到了引用类型的对象究其原因,这是重要的是,.NET类型可分为以种类的类型-引用类型值类型

值类型之类的东西内置数值类型([int][double][decimal][bool]等) -但也更复杂的数据类型,如[datetime],通过返回对象的类型Get-Date

这种区别(引用与值)很重要的原因是,它们在分配和存储时的行为有所不同。对于值类型,任何赋值都将导致.NET将对象的值复制到赋值目标:

$a = 1    
$b = $a   # Assign value of $a to $b
$a++      # Modify value of $a
$b        # Is $b 1 or 2?

在赋值操作期间$b = $a,PowerShell查看$a,发现其值1属于类型[int]并存储值1 in的新副本$b-因此,即使我们将值$a增加到2,其值也$b不会改变。

对于引用类型,情况并非如此。引用类型对象通过引用进行存储和传递

$a = [System.Diagnostics.ProcessStartInfo]::new("calc.exe")
$b = $a
$a.FileName = "notreally.exe"
$b.FileName             # Is $b.FileName "calc.exe" or "notreally.exe"?

在这种情况下,由于[System.Diagnostics.ProcessStartInfo]是引用类型,$a不包含(即与对象关联的数据),因此仅包含引用指针指向数据实际存储在内存中的位置,而分配$b = $a不复制对象,而不是两个$a$b两端向上引用相同的底层对象,并通过其他询问时一个的修改被反射。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法从字符串转换为System.Diagnostics.ProcessStartInfo C#

来自分类Dev

System.Diagnostics.Trace,System.Diagnostics.Debug和System.Console有什么区别?

来自分类Dev

如何使用PowerShell和System.IO.StreamWriter编写Linux换行符

来自分类Dev

System.Diagnostics.Trace与ILogger

来自分类Dev

性能:System.Diagnostics.Process与System.Management.Automation.PowerShell

来自分类Dev

使用System.IO.StreamWriter和长字符串Json的C#文件限制

来自分类Dev

ProcessStartInfo.EnvironmentVariables和ProcessStartInfo.Environment有什么区别

来自分类Dev

System.Diagnostics.Trace或System.Diagnostics.WriteLine和CRM 2011插件

来自分类Dev

System.Diagnostics.Trace或System.Diagnostics.WriteLine和CRM 2011插件

来自分类Dev

执行Powershell时出错:System.Diagnostics.Process.StartWithShellExecuteEx

来自分类Dev

System.Diagnostics.Process.Start()参数dotnet和diff

来自分类Dev

领域和包含System.Diagnostics.Debug nuget包

来自分类Dev

单声道中的System.Diagnostics.Tracing

来自分类Dev

System.Diagnostics.Debug.WriteLine被跨接

来自分类Dev

单声道中的System.Diagnostics.Tracing

来自分类Dev

使用System.Diagnostics.PresentationTraceSources.SetTraceLevel()方法

来自分类Dev

如何将System.Diagnostics.Trace和System.Diagnostics.Debug消息记录到NLog文件?

来自分类Dev

PowerShell [System.IO.StreamWriter]写入控制台(写入主机)?

来自分类Dev

System.IO.StreamWriter:为什么必须使用关键字“ using”

来自分类Dev

如何在C#中使用System.Diagnostics.Process.Start运行PowerShell脚本?

来自分类Dev

控制System.Diagnostics.Process.Start C#的操作

来自分类Dev

在Release版本中获得System.Diagnostics.Debug.Assert()的行为?

来自分类Dev

如何创建和使用System.Diagnostics.PerformanceData.CounterSet?

来自分类Dev

System.Diagnostics.Trace-记录异常的正确方法

来自分类Dev

我的项目无法使用System.Diagnostics.Process

来自分类Dev

System.Diagnostics.Trace,最简单的程序配置

来自分类Dev

从xml文件加载System.Diagnostics.TraceSource的配置

来自分类Dev

使用System.Diagnostics.Conditional从库中排除方法

来自分类Dev

等效于PCL库中的System.Diagnostics.Trace

Related 相关文章

  1. 1

    无法从字符串转换为System.Diagnostics.ProcessStartInfo C#

  2. 2

    System.Diagnostics.Trace,System.Diagnostics.Debug和System.Console有什么区别?

  3. 3

    如何使用PowerShell和System.IO.StreamWriter编写Linux换行符

  4. 4

    System.Diagnostics.Trace与ILogger

  5. 5

    性能:System.Diagnostics.Process与System.Management.Automation.PowerShell

  6. 6

    使用System.IO.StreamWriter和长字符串Json的C#文件限制

  7. 7

    ProcessStartInfo.EnvironmentVariables和ProcessStartInfo.Environment有什么区别

  8. 8

    System.Diagnostics.Trace或System.Diagnostics.WriteLine和CRM 2011插件

  9. 9

    System.Diagnostics.Trace或System.Diagnostics.WriteLine和CRM 2011插件

  10. 10

    执行Powershell时出错:System.Diagnostics.Process.StartWithShellExecuteEx

  11. 11

    System.Diagnostics.Process.Start()参数dotnet和diff

  12. 12

    领域和包含System.Diagnostics.Debug nuget包

  13. 13

    单声道中的System.Diagnostics.Tracing

  14. 14

    System.Diagnostics.Debug.WriteLine被跨接

  15. 15

    单声道中的System.Diagnostics.Tracing

  16. 16

    使用System.Diagnostics.PresentationTraceSources.SetTraceLevel()方法

  17. 17

    如何将System.Diagnostics.Trace和System.Diagnostics.Debug消息记录到NLog文件?

  18. 18

    PowerShell [System.IO.StreamWriter]写入控制台(写入主机)?

  19. 19

    System.IO.StreamWriter:为什么必须使用关键字“ using”

  20. 20

    如何在C#中使用System.Diagnostics.Process.Start运行PowerShell脚本?

  21. 21

    控制System.Diagnostics.Process.Start C#的操作

  22. 22

    在Release版本中获得System.Diagnostics.Debug.Assert()的行为?

  23. 23

    如何创建和使用System.Diagnostics.PerformanceData.CounterSet?

  24. 24

    System.Diagnostics.Trace-记录异常的正确方法

  25. 25

    我的项目无法使用System.Diagnostics.Process

  26. 26

    System.Diagnostics.Trace,最简单的程序配置

  27. 27

    从xml文件加载System.Diagnostics.TraceSource的配置

  28. 28

    使用System.Diagnostics.Conditional从库中排除方法

  29. 29

    等效于PCL库中的System.Diagnostics.Trace

热门标签

归档