468,743 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,743 developers. It's quick & easy.

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(object 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
Apr 9 '08 #1
2 1405
Plater
7,872 Expert 4TB
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.
Apr 9 '08 #2
r035198x
13,262 8TB
Have a look at the Command pattern.
Apr 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by John J. Lee | last post: by
5 posts views Thread by Ulf Rimkus | last post: by
4 posts views Thread by Fedor Semenov | last post: by
25 posts views Thread by paytam | last post: by
10 posts views Thread by Amber | last post: by
16 posts views Thread by Stefano Sabatini | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.