patterncsharpMinor
Naive implementation of attached properties for WinForms
Viewed 0 times
naiveattachedpropertieswinformsforimplementation
Problem
I need to implement something like "attached properties" from WPF that targets WinForms.
What I came up with seems to work. Can you find any issues with it? The helper class and example are shown below.
Note:
-
This is experimental code that I'm doing to port Prism to WinForms. There are very few changes needed to do the port, but unfortunately to really complete the job I just need something like attached properties.
-
The code lives at github.com/misct/prism-winforms where I have already posted a slightly improved implementation and where I will soon post a much better implementation.
ExtenderHelper.cs
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace TryAttachedProps
{
public static class ExtenderHelper
{
#region Property Data Classes
class AttachedProperty
{
public Func Getter;
public Action Setter;
public object Value;
}
class AttachedPropertyMap : Dictionary { }
#endregion
static Dictionary _attachedProperties =
new Dictionary();
public static void AttachProperty(this Component component, string name, Func getter)
{
AttachProperty(component, name, getter, ReadOnlySetter);
}
public static void AttachProperty(this Component component, string name, Func getter, Action setter)
{
AttachedPropertyMap props;
if (!_attachedProperties.TryGetValue(component, out
What I came up with seems to work. Can you find any issues with it? The helper class and example are shown below.
- Not thread-safe since all access should be performed from the UI thread.
- Attach a value (SetAttachedValue) or getter/+setter (AttachProperty).
- All properties are un-attached when the target component is disposed.
- The primary use-case is that an IExtenderProvider will attach properties to various controls/components of a form and then other code will be able to access those attached properties without having to have a reference to the original IExtenderProvider.
Note:
-
This is experimental code that I'm doing to port Prism to WinForms. There are very few changes needed to do the port, but unfortunately to really complete the job I just need something like attached properties.
-
The code lives at github.com/misct/prism-winforms where I have already posted a slightly improved implementation and where I will soon post a much better implementation.
ExtenderHelper.cs
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace TryAttachedProps
{
public static class ExtenderHelper
{
#region Property Data Classes
class AttachedProperty
{
public Func Getter;
public Action Setter;
public object Value;
}
class AttachedPropertyMap : Dictionary { }
#endregion
static Dictionary _attachedProperties =
new Dictionary();
public static void AttachProperty(this Component component, string name, Func getter)
{
AttachProperty(component, name, getter, ReadOnlySetter);
}
public static void AttachProperty(this Component component, string name, Func getter, Action setter)
{
AttachedPropertyMap props;
if (!_attachedProperties.TryGetValue(component, out
Solution
WinForms is not WPF.
Sad, boring truth. The correct way of extending WinForms controls is, as was mentioned, through inheritance.
What you've got here is a set of extension methods in a dual-purpose
That said...
You're exposing
I think the static class with the extension methods should be called
I can't think of other ways to do this though... because I don't think I get the point. If you want to do WPF, drop WinForms and do WPF, don't try to turn a Corolla into an Audi!
Sad, boring truth. The correct way of extending WinForms controls is, as was mentioned, through inheritance.
What you've got here is a set of extension methods in a dual-purpose
static class that's asking for trouble in the sense that it's also a state-holding bag of static objects that have "attached properties" - I think this code is abusing extension methods and static classes, be it only because the static class itself has two non-static "child" classes.That said...
class AttachedProperty
{
public Func Getter;
public Action Setter;
public object Value;
}You're exposing
object, which incurs boxing of value types. And then you're storing state in a dictionary of dictionaries - which is a code smell IMO (begging for a type/class here).I think the static class with the extension methods should be called
ComponentExtensions, but its dual purpose defeats that. I think the class is breaking SRP.I can't think of other ways to do this though... because I don't think I get the point. If you want to do WPF, drop WinForms and do WPF, don't try to turn a Corolla into an Audi!
Code Snippets
class AttachedProperty
{
public Func<Component, object> Getter;
public Action<Component, object> Setter;
public object Value;
}Context
StackExchange Code Review Q#42540, answer score: 4
Revisions (0)
No revisions yet.