0

There are many codes in C# that use the keywords using and new. They look very simple!

How to achieve that in in PowerShell elegantly!

For example I want to change the following C# code to PowerShell

using (var image = new MagickImage(new MagickColor("#ff00ff"), 512, 128))
{
    new Drawables()
      // Draw text on the image
      .FontPointSize(72)
      .Font("Comic Sans")
      .StrokeColor(new MagickColor("yellow"))
      .FillColor(MagickColors.Orange)
      .TextAlignment(TextAlignment.Center)
      .Text(256, 64, "Magick.NET")
      // Add an ellipse
      .StrokeColor(new MagickColor(0, Quantum.Max, 0))
      .FillColor(MagickColors.SaddleBrown)
      .Ellipse(256, 96, 192, 8, 0, 360)
      .Draw(image);
}

It is difficult to write the new expression because it contains one another! What elegant solution is there?

enter image description here

3
  • @Claies C# and PowerShell have many similarities. PS can use C# class library, but some places are not easy to convert, such as the above Commented Dec 12, 2020 at 5:46
  • @Claies both run on the .NET framework. Powershell can do everything other .NET languages like F#, C#, VB.NET... can and vice versa Commented Dec 12, 2020 at 5:46
  • Using the following tools, you can convert between C# and PowerShell, but the effect of complex code is not very good,ironmansoftware.com/powershell-pro-tools-powershell-module Commented Dec 12, 2020 at 5:54

1 Answer 1

1

C# using is just syntactic sugar for try {} finally {} so you can do the same in PowerShell. The disposal of the object will be put in the finally block. new can be replaced with New-Object. Of course new can be made an alias to New-Object but MS chose not to

try {
    $color = New-Object MagickColor -ArgumentList (,"#ff00ff")
    $image = New-Object MagickImage -ArgumentList ($color, 512, 128)
    $drawable = New-Object Drawables
    $drawable.FontPointSize(72). `
              Font("Comic Sans"). `
              StrokeColor(New-Object MagickColor -ArgumentList (,"yellow")). `
              FillColor()...
}
finally {
    if ($image) { $image.Dispose() }
    # or just `$image?.Dispose()` in PowerShell 7.1+
}

See

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your analysis of keyword【using】. Now, it is difficult to understand the keyword【new】 in the code above, because one contains another
@tianyi there's nothing special about those nested new. They're just R-Value Expressions that you can store in a variable (see my edit) or pass directly as arguments to other functions like this $image = New-Object MagickImage -ArgumentList ((New-Object MagickColor -ArgumentList (,"#ff00ff")), 512, 128)
.Font("Comic Sans") This should not be supported by powershell, Cannot start with.
@tianyi if FontPointSize returns a Drawables then obviously .Font will be called correctly. Functions and methods are called in powershell just like in C#. It's just that the new line ends the command so you need to escape it with backticks. See the updated code
@tianyi just found out that the member access operator must immediately follow the object so just move the dot to the upper line
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.