Wednesday, November 26, 2008

Protected Dependency Properties Are Not Really

I discovered an interesting fact yesterday.  I was attempting modify ImplicitStyleManager to support the DefaultStyleKey property.  Previously I was just using the concrete type of the object I was styling to retrieve its style from the resource dictionary.  Unfortunately this prevented types derived from System.Windows.Controls.Button from getting styled as buttons.  This diverged from WPF's behavior and understandably we got flack for it on CodePlex.  When I saw that the property was protected I thought "game over."  Luckily Silverlight Toolkit team member Justin Angel didn't give in so easily.

Justin: "Lemme try something."

Me: "Justin we can't access protected members.  I mean we could add an interface or something..."

Justin: "Just gimme a sec."

After a few moments he IM'ed me something very much like this:

internal class DefaultStyleKeyRetriever : Control { /// <summary> /// Initializes a new instance of the DefaultStyleKeyRetriever class. /// </summary> public DefaultStyleKeyRetriever() { } /// <summary> /// This method retrieves the default style key of a control. /// </summary> /// <param name="control">The control to retrieve the default style key /// from.</param> /// <returns>The default style key of the control.</returns> public object GetDefaultStyleKey(Control control) { return control.GetValue(Control.DefaultStyleKeyProperty); } }

It worked of course.  DefaultStyleKey is a dependency property.  Another class that inherits from Control cannot access a protected CLR property that belongs to another Control instance.  However any class that inherits from Control has access to all of its static members, one of which was the DefaultStyleKeyProperty dependency property object.  Since CLR properties and dependency properties usually come in pairs that means that effectively any protected properties that use a DP as a backing store are not as protected as you may think.

FYI.

10 comments:

Anonymous said...

Interesting post.
However, I'm not sure it is right behavior or not. Is it not a sort of "HOLE" of class security?

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Ha! That's a good trick to know.

Affordable Luxurious Wedding Dress Blog said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.

About Me

My photo
I'm a software developer who started programming at age 16 and never saw any reason to stop. I'm working on the Presentation Platform Controls team at Microsoft. My primary interests are functional programming, and Rich Internet Applications.