How can I add a method to an existing pre-defined class in VS2005 .Net?

Posted on 2008-11-19
Last Modified: 2012-05-05
I'm working on a large-scale (vb) project, and  I'm wondering if there's a way to add a new method to the Control class.

Inheritance isn't really an option -- if I made ControlEx Inherits Control and added it there, I'd then need to create a WebControlEx, LabelEx, DropDownList, etc to inherit from that.

I tried creating a Partial Public Class Control and adding the method there (well, okay, I added a simple property for testing purposes), but it didn't work -- I'm pretty sure that's just creating a new "Control" class and not even trying to append to the pre-defined one.

I know this is possible in scripting languages like Javascript and Lua, but has anyone had any success with this in .Net?
Partial Public Class Control

    Private _test As Integer

    Public Property Test() As Integer


            Return _test

        End Get

        Set(ByVal value As Integer)

            _test = value

        End Set

    End Property

End Class

Open in new window

Question by:Merion
    LVL 9

    Assisted Solution

    Have you considered using extension methods?
    LVL 13

    Assisted Solution

    dotnet framework 3,0 allows you to add static methods to existing classes but this is not available in framework 2.0

    for more info and samples

    Author Comment

    We're running 2.0 SP1 so I don't think either of those things are available.

    And it can't be static regardless.  Specifically, I'm looking to add something akin to a FindControlRecursive method that will recurse through the control hierarchy to find the control with the specified ID. The method is made and implemented for one control type, but I'd like to apply it to the Control class so that all controls can use it.
    LVL 3

    Accepted Solution

    In .NET 2.0 it's impossible.
    It became possible only since .NET 3.0
    The only way to extend functinoality in .NET 2.0 and lower is to inherit from the class.

    There are another ways:
    Way 1. Create Composite control:
    1. You create an Interface with method you need (FindControlRecursive)
    2. You create Control classes you need, inherit them from necessary control classes (e.g. from UserControl, Label etc)
    3. Implement created previously interface with necessary method - you may even just call static implementatino or do it another appropriate way.

    Way 2. Use AOP:
    Use Aspect Oriented Programming: use proxies. Just using any AOP engine wrap controls you need and use this method.

    Way 3. Consider another way of implementation.
    Why do you need to have this method in your controls. May be it's possible to move it to another place? What is your task?
    LVL 9

    Expert Comment

    As an alternative to FindControlRecursive you could use this which uses generics:

    You can read more about it here

    public static T FindControl<T>(System.Web.UI.ControlCollection Controls) where T : class
         T found = default(T);
         if (Controls != null && Controls.Count > 0)
              for (int i = 0; i < Controls.Count; i++)
                   if (Controls[i] is T)
                        found = Controls[i] as T;
                        found = FindControl<T>(Controls[i].Controls);
         return found;

    Open in new window


    Author Comment

    While that's a good example of finding a control that matches a specific type, I'm not sure what it has to do with my question. :-)
    The function I wrote matches by ID -- just like Control.FindControl does.

    Anyhow, thanks to everyone for your help.  It was kind of a "nice-to-have" feature so I'm not going to worry about it any further.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    27 Experts available now in Live!

    Get 1:1 Help Now