Refactor/Remove `PropertyStore`
See original GitHub issueBackground and motivation
We have had a few bugs caused by incorrect usage of PropertyStore. It’s clunky and we can use private backing fields instead. This would allow nullable annotations and compile time checks which would have prevented #8990
Also can look into using a dictionary instead to gain perf improvements. But
Issue raised based on comment: https://github.com/dotnet/winforms/pull/9503#pullrequestreview-1533416827
API Proposal
internal class PropertyStore
{
private sealed class ValueWrapper<T> where T : struct
{
public T Value { get; }
public ValueWrapper(T value)
{
Value = value;
}
}
private readonly Dictionary<int, object?> properties = new();
private static int s_currentKey = 0;
public static int CreateKey() => s_currentKey++;
public void SetValue<T>(int key, T value) where T : struct
{
properties[key] = new ValueWrapper<T>(value);
}
public void SetObject<T>(int key, T value)
{
properties[key] = value;
}
public T GetValue<T>(int key) where T : struct
{
if (properties.TryGetValue(key, out var value) && value is ValueWrapper<T> wrapper)
{
return wrapper.Value;
}
return default;
}
public T? GetObject<T>(int key)
{
return properties.TryGetValue(key, out var value) && value is T tValue ? tValue : default;
}
public bool TryGetValue<T>(int key, out T? value) where T : struct
{
if (properties.TryGetValue(key, out var objValue) && objValue is ValueWrapper<T> wrapper)
{
value = wrapper.Value;
return true;
}
value = default;
return false;
}
public bool TryGetObject<T>(int key, out T? value) where T : class
{
if (properties.TryGetValue(key, out var objValue) && objValue is T tValue)
{
value = tValue;
return true;
}
value = null;
return false;
}
public bool Remove(int key) => properties.Remove(key);
}
API Usage
N/A
Alternative Designs
No response
Risks
It is a large refactor and could cause subtle bugs. The current design is meant for performance reasons (I don’t understand why?).
Will this feature affect UI controls?
Controls will need testing.
Issue Analytics
- State:
- Created 2 months ago
- Reactions:1
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Disposing of an MDI parent form twice throws an exception
Dispose() twice inside a form that has a child MDI form crashes the application with NullReferenceException . Inspecting platform code code ...
Read more >PSCreateMemoryPropertyStore function (propsys.h)
This function creates an in-memory property store object that implements IPropertyStore, INamedPropertyStore, IPropertyStoreCache, ...
Read more >Changelog — Scala IDE 0.1-SNAPSHOT documentation
... Refactor/remove todos; Set UTF-8 as default charset in test workspaces; Enable compiler ... Replaces PropertyStore with a set of sparser platform calls....
Read more >Simple promise-based session middleware for Next.js, ...
next-session accepts the properties below. ... Refactor, remove rolling option and fix unreliable tests (#283); Avoid override set-cookie ...
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

@elachlan
ValueWrapper<T>is more trouble than it is worth. I was thinking of going as simple as possible:As mentioned in the comments above we really need to look at how much memory we’re actually saving with the current implementation. If we have 1000 live controls and are saving 32 bytes, that would be 32K of memory. 10,000 we’d save 320K. If we’re not saving several megabytes for 1000 controls, I don’t know that this complexity is worth maintaining. I’d rather see us gain the speed and simplicity and spend our time finding other ways to better use memory.
@merriemcgaw can you please tag this to .NET9? Also I don’t think it is an API suggestion because
PropertyStoreisInternal? Do we need a full review or just a team sign off on the plan?