Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrap a method in a try..finally using an attribute 1

Status
Not open for further replies.

zythra

Programmer
Mar 31, 2005
5
US
I'd like to create an attribute that I can use to wrap a method in a try..finally. I don't know if this is possible, but any ideas would be appreciated. Basically I'd like to be able to do the following:

[TryFinally]
public void MyMethod() {
dostuff;
}

And the code of the method would be wrapped in a try..finally without actually having to add the try..finally in the method.
 
Instead of this:

public void MyMethod() {
try {
dostuff;
} finally {
do finally stuff;
}
}

I want to do this:

[TryFinally]
public void MyMethod() {
dostuff;
}
 
Have a look at this:


I have no idea what you are trying to do here, but I still don't think you can do it. In your second example, how will your program ever know to 'do finally stuff'?

What exactly is your problem with the first example, besides that it works? ;-)

Good Luck,

Alex

[small]----signature below----[/small]
I can't compete with you physically, and you're no match for my brains.
You're that smart?
Let me put it this way. Have you ever heard of Plato, Aristotle, Socrates?
Yes.
Morons!
 
Alex, your response cracked me up. :) I know very well how to use a try..finally. I really don't know if what I'm after is possible. Here's the jist. In winForms I have methods that have a try..finally in them where the finally is always the same thing. I'm just looking for a way to make it so I can just add an attribute to a method without having to add the try..finally.

I know, I'm just being lazy. I've done attributes, but nothing like what I'm trying t attempt here, because I really don't know if this is something that could be done.

Yes, the first example works perfectly, I'm just trying to get out of adding the full try..finally every time. :) Lazy, lazy, lazy. ;-)
 
Aha!

Why don't you put each method you're trying to call into an Interface. then you call that interface method in a try catch. If you want more detail - google C# Action Class

an example interface would be

public interface iAction
{
bool PerformAction();
void PerformAlternate();
}

Then your calling class would do this for each class

iActionclass = new SomeClass(); //someclass inherits iAction

public bool RunAction(iAction action)
{
try
{
action.PerformAction();
}
catch (Exception ex)
{
//log your exception
action.PerformAlternate();
}
}

 
I like your thoughts there JurkMonkey, unfortunately it's not quite what I'm looking for. If the action I needed to perform between the try and the finally was the same every time, it would work. You did give me some thoughts for something else though so I gave you a star. :)

If anything, this thread has put a few smiles on my face. I'm starting to think I can't do what I'm wanting. I actually had further intentions with this type of attribute other than a try..finally, but I'm starting to think it won't work. I had some thoughts with reflection. If I get anywhere I'll post it in case anyone is curious.

One thing I would like to do is use an attribute to wrap the logic of a method in a using{} as well. Like so:

This:

public void MyMethod() {
using (Type t = new Type()) {
doSomeStuff;
t.DoSomething();
}
}

Would end up like this:

[UsingAttribute]
public void MyMethod() {
doSomeStuff;
}

Maybe this example will give someone some ideas that the try..finally didn't. If not, The thread has still been enjoyable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top