By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

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

P: 1
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
Share this Question
Share on Google+
2 Replies


Plater
Expert 5K+
P: 7,872
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

10K+
P: 13,264
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.