Welcome to Devintelligence.com Sign in | Join | Help

.Net Adventures

In .Net we trust
Your Cheap Web Hosting Solution


Tag property without inheritance

Tag property is very often used to store a data associated with an object. The simple way to add support for Tag property is to create a base class with the Tag property .I don’t like this solution – the reason is simple .Sometimes I want to inherit from an existing class that doesn’t have the Tag property and I can’t change its source code .In WPF we have dependency properties that can help us in this case.

MyControl cnt = new MyControl();
cnt.SetValue(FrameworkElement.TagProperty, new Info() );

One problem – we still need to inherit from DependecyObject class.
The helper class below uses extension methods – to provide the tag functionality for your classes.
Of course, for better performance, you may change the dictionary(and methods) to be more type specific .

/// <summary>
    /// Implements tag functionality
    /// </summary>
    static class TagHelper
    {

        /// <summary>
        /// Adds the tag.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="tag">The tag.</param>
        public static void AddTag(this object element, object tag)
        {
            if (!tags.ContainsKey(element))
            {
                tags.Add(element, tag);
            }
        }

        /// <summary>
        /// Removes the tag for the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public static void RemoveTag(this object element )
        {
            tags.Remove(element);
        }

        /// <summary>
        /// Clears the tags.
        /// </summary>
        public static void ClearTags()
        {
            tags.Clear();
        }

        /// <summary>
        /// Gets the tag for the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static object Get(this object element)
        {
            object tag;
            tags.TryGetValue(element, out tag);

            return tag;
        }

        private static Dictionary<object, object> tags = 
            new Dictionary<object, object>(); 
    }

 

Technorati Tags: ,,
Posted: Tuesday, April 22, 2008 1:20 PM by admin

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 22, 2008 5:43 AM

Tomer said:

Note that you might have a memory leak in that code, because the dictionary is static has a reference to your object.

You could solve that using WeakReference though.

# April 22, 2008 7:16 AM

John "Z-Bo" Zabroski said:

WPF allows more than just Tag Properties.  Tag Properties are actually a WinForms concept.  WPF allows much richer properties to be defined: Attached Properties.

See WPF Unleashed by Nathan: "Just like previous technologies such as Windows Forms, many classes in WPF define a Tag property (of type System.Object) intended for storing arbitrary custom data with each instance. But attached properties are a more powerful and flexible mechanism for attaching custom data to any object deriving from DependencyObject. It's often overlooked that attached properties enable you to effectively add custom data to instances of sealed classes (something that WPF has plenty of)!" http://www.codeguru.com/csharp/sample_chapter/print.php/c13347__4/

# April 23, 2008 9:55 PM

Rob Relyea - Xamlified said:

WPF Applications Karsten: Using Flotzam At A Conference .&#160; That plus a cool way to pick sessions

# April 25, 2008 5:41 AM

admin said:

To John "Z-Bo" Zabroski: I just tried to create a lightweight version of class that supports Tag property without using DependencyObject .

# April 30, 2008 11:39 AM

Ultram er. said:

Ultram er. Information on ultram er. How people abuse ultram er. Ultram-er.

# June 15, 2008 8:24 PM

Buy carisoprodol. said:

Buy carisoprodol online lowest price guarantee. Buy carisoprodol.

# June 21, 2008 1:03 AM

Celexa. said:

Celexa.

# July 11, 2008 3:51 PM

Free motorola v220 ringtones. said:

Motorola ringtones free.

# September 19, 2008 9:01 PM
New Comments to this post are disabled