What is the C# for this C++

N

newscorrespondent

C++

void SomeClass::SomeRoutine (void)
{ QuestionClass abc(__FUNCTION__);
}

In C++ when "SomeRoutine" runs the QuestionClass gets the name of the
current function. It seems like I should be able to get this using
reflection with C# but I have not been able to figure out how. Is there a
way to do this?

When this routine ends the destructor for QuestionClass is run. Is there a
way in C# to insure that the class is destroyed in a similar fashion. The
best way I can see is to make the class implement IDisposable and use a
using clause wrapping the entire contents of the routine. Is there a better
way?

Thanks
 
C

Carl Daniel [VC++ MVP]

C++

void SomeClass::SomeRoutine (void)
{ QuestionClass abc(__FUNCTION__);
}

In C++ when "SomeRoutine" runs the QuestionClass gets the name of the
current function. It seems like I should be able to get this using
reflection with C# but I have not been able to figure out how. Is
there a way to do this?

See System.Environment.StackTrace or the System.Diagnostics.StackTrace
class. There's an example at

http://msdn2.microsoft.com/en-us/4ce0ktkk(VS.80).aspx

that shows how to get the current routine name using the StackTrace class.
When this routine ends the destructor for QuestionClass is run. Is
there a way in C# to insure that the class is destroyed in a similar
fashion. The best way I can see is to make the class implement
IDisposable and use a using clause wrapping the entire contents of
the routine. Is there a better way?

No - that's the best you can do in C#.

-cd
 
B

Barry Kelly

C++

void SomeClass::SomeRoutine (void)
{ QuestionClass abc(__FUNCTION__);
}

In C++ when "SomeRoutine" runs the QuestionClass gets the name of the
current function. It seems like I should be able to get this using
reflection with C# but I have not been able to figure out how. Is there a
way to do this?
MethodBase.GetCurrentMethod()

When this routine ends the destructor for QuestionClass is run. Is there a
way in C# to insure that the class is destroyed in a similar fashion. The
best way I can see is to make the class implement IDisposable and use a
using clause wrapping the entire contents of the routine. Is there a better
way?

No, that's the standard C# idiom for this.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top