question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hashtable Conversion Support Constructor Parameters

See original GitHub issue

Summary of the new feature / enhancement

I would like the ability to cast from a hashtable using the constructor of a type, for example when no parameterless constructor is available but even if one is, using a ‘.ctor’ key in the hashtable.

The ‘.ctor’ key would be interpreted as an array and passed to the constructor of the type. I would expect similar if not identical behavior with this key’s presence as observed using the New-Object -ArgumentList $ConstructorParameters.

Proposed technical implementation details (optional)

# Class definition used for functionality examples
class TestClass {
  hidden static [int]$NextId
  [int]$Id
  [string]$Name
  [object]$Payload
  # Because there is no parameterless constructor, currently this class cannot be instantiated by cast from a hashtable
  TestClass([string]$name) {
    $this.Id = ++[TestClass]::NextId
    $this.Name = $name
  }
}

# The three test cases below each construct the TestClass class and set the value of the Payload property

# TestCase1 demonstrates how we would do this currently (without New-Object)
$TestCase1 = [TestClass]::new("FirstTest")
$TestCase1.Payload = 1

# TestCase2 demonstrates how New-Object already supports this behavior
$TestCase2 = New-Object -TypeName TestClass -ArgumentList SecondTest -Property @{Payload  = 2}

# TestCase3 demonstrates how this operation would be performed using the '.ctor' hashtable key
$TestCase3  = [TestClass]@{
  '.ctor'   = @('ThirdTest')
  'Payload' = 3
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
vexx32commented, Oct 29, 2021

Yeah, it’s tricky to make that look right. I think the original syntax proposed works pretty well, though I don’t know about using .ctor for that, it’s not something most powershell folks will be familiar with / understand too well, I think. Maybe we keep the ::new for that or something? 🤔

[TestClass]@{
    ::new = 'TestName'
    Payload = 7
}

I would also suggest whatever use for the constructor key should be able to use either an array for positional parameters, or a hashtable to provide named constructor parameters:

[TestClass]@{
    ::new = @{
        name = 'TestName'
    }
    Payload = 7
}
2reactions
SeeminglySciencecommented, Oct 29, 2021

It may be worth considering formalizing initialization syntax, e.g.

[TestClass]::new('ThirdTest') @{ Payload = 3 }

The introduction of init-only properties to C# makes this a lot more attractive than it’s been in the past.

It should be pretty rare for the above syntax to behave differently than expected, though dynamic static invocations in command parsing may be confusing:

$type = [TestClass]
Write-Output $type::new('ThirdTest') @{ Payload = 3 }

But non-dynamic will be fine since type expressions aren’t parsed in command parsing anyway. I believe in all other scenarios this syntax would result in a parse error today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Dictionary or hashtable as a parameterized constructor ...
I am working on wpf application and i have to pass some global objects from one class to other, so i am declaring...
Read more >
Hashtable Constructor (System.Collections)
Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object.
Read more >
Hashtable (Java Platform SE 8 )
An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets...
Read more >
about Hash Tables - PowerShell
A hashtable, also known as a dictionary or associative array, is a compact data structure that stores one or more key-value pairs.
Read more >
Hashtable in Java - Scaler Topics
The Hashtable class creates a hash table by mapping keys to values. In a hashtable, any non-null object can be used as a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found