Hashtable Conversion Support Constructor Parameters
See original GitHub issueSummary 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:
- Created 2 years ago
- Reactions:1
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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? 🤔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:
It may be worth considering formalizing initialization syntax, e.g.
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:
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.