C#-APP Is there any way to "redefine" a class legally ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeff123
    New Member
    • Apr 2008
    • 1

    C#-APP Is there any way to "redefine" a class legally ?

    Hi all,

    Recently I started using C#, I have a question: Can we redefine a class legally in C# ?

    For example:

    private void button1_Click(o bject sender, EventArgs e)
    {
    MessageBox.Show ("testing");
    }

    When the user click the button, it shows up a message box

    If one day I want to log this action temporary, or prevent all the MessageBox from showing up entirely in my application, how can I make it happen without modifying all the MessageBox.Show () in my code ?

    I know that I could make a subclass from MessageBox , use override or New, but it is not what I want. I am looking for a language facility for changing the behavior of any class (any method) dynamically in my namespace.

    For example, I imagine that there is a keyword, let say, "OnEvent"

    So that I can write some magic code like this:

    OnEvent MessageBox Show( )
    {
    myClass.log( );
    this.Show( );
    }

    after that, whenever I call Show() method from MessageBox class, the method actually got "hijacked" and under my control.

    Any advice or comment is also welcomed.

    Jeff
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I want to say the latest versions of .NET (3.0 and greater) have limited implimentation of this kind of thing, but I don't have .NET3.0 and couldn't test it.
    As an in general I would say that no (unless you can find some sneaky Win32 API to do it) there is not really any good .net way to do it.

    Which is why I start out all my project with creating a special function that I call from everywhere. Then inside that function I can dictate if I want it to log or show a messagebox or anything else. Abstracting out the IO is not a bad thing to do.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Have a look at the Command pattern.

      Comment

      Working...