Extending functionality for .NET controls

I'm currently working on a tool that should create some kind of custom .NET with some additional fields. I was wondering which approach is better to solve, and thought about the following options:

Option A: - Create a derived class for each control (say, CLabel, CGroupBox ...), where I define new fields for each class. This will mean worse compatibility, but it will be easy to work with.

Example:

class CLabel: Label
{
    public List<Control> RelatedControls {get; set;}
    public String textHint;
    // more stuff...

    public CLabel()
    {}
 }

Option B: - This parameter does not mean creating a derived class from each control, but using the actual Label, GroupBox, etc. controls. And creating a class that encapsulates all the "extra" properties. This additional property object will be referenced in the property Control.Tag. I doubt it, because the reference to some complex object inside the property Tagseems a little crap to me, but it will mean better compatibility, and also, of course, there is no need for subclass elements.

Example:

Label lbl = new Label();
lbl.Tag = new ControlDescription();

C: - - A B. Custom Control, CLabel, ControlDescription Label. , Tag.

, , , . , - , , - . , , ? , ?

+5
3

, . - (Control, Value), , , .

:

public static class Extension
{
    private static Dictionary<Control, string> _controlStrings;

    public static void GetString(this Control ctrl)
    {
        return _controlStrings[ctrl];
    }
 }

, :

Button btn = new Button();
string s = btn.GetString();
+1

, :

public class ExtensionData
{
    //put your data here
}

public class Extended<T>
    where T : Control
{
    public Extended( T baseControl )
    {
        BaseControl = baseControl;
    }

    public T BaseControl { get; set; }

    public ExtensionData Extension { get; set; }
}

This is similar to your "option B", but without using the "Tag" property

0
source

All Articles