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

I'm working on a large-scale asp.net (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
        Get
            Return _test
        End Get
        Set(ByVal value As Integer)
            _test = value
        End Set
    End Property
 
End Class

Open in new window

MerionAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amxCommented:
Have you considered using extension methods?
0
TechTiger007Commented:
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
http://msdn.microsoft.com/en-us/library/bb383977.aspx
http://weblogs.asp.net/dwahlin/archive/2008/01/25/c-3-0-features-extension-methods.aspx
0
MerionAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ASliborskyCommented:
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?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amxCommented:
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;
                    break;
               }
               else
                    found = FindControl<T>(Controls[i].Controls);
          }
     }
 
     return found;
}

Open in new window

0
MerionAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.