|
Hello,
I am looking to find a way to retrieve the name of the method that called the current method. At first I thought there would be something within System.Reflection but unfortunately the only thing I can find is MethodInfo.GetCurrentMethod() which retrieves the name of the method I am currently in.
The reason for this is that I am in the process of creating a logging class, one of the logging functions allows the caller to log that a particular method has been called/exited. For ease and control I would prefer not to have to pass the function name/namespace as parameters. i.e.
private void MyFunction()
{
LogFunctionEntry(MethodInfo.GetCurrentMethod())
}
public static void FunctionEntry(MethodInfo mi)
{
string message = string.Format(
System.Globalization.CultureInfo.InvariantCulture,
"{0}.{1}",
mi.ReflectedType,
mi);
Console.WriteLine(message);
}
At the moment the only other way I think of doing this is to parse through Environment.StackTrace but to be honest this is something I would rather not do.
|
|
|
|
|
The information is in the call stack, so it's from there that you have to get it.
Perhaps there are some other methods that you can use that does some of the work for you. For an example, if you throw an exception and catch it, it will contain a stack trace. Throwing exceptions is quite expensive, though.
---
b { font-weight: normal; }
|
|
|
|
|
Typically I have found the answer to my question within minutes of making the post
Annoyingly it was right here on CP
http://www.codeproject.com/dotnet/MethodName.asp[^]
Solution:
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
string message = string.Format(
System.Globalization.CultureInfo.InvariantCulture,
"{0}|{1}",
methodBase.ReflectedType,
methodBase);
Console.WriteLine(message);
This will output
namespace.function(params)
MyNameSpace.Foo.Bar|MyFunction(object a, object b)
|
|
|
|
|
Aaah, thanks for sharing the info. I found it interesting too.
Did you vote for that kind soul's article?
Cheers,
Vikram. "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
|
|
|
|
|
oh yes
Voting is good and I get to feel all warm and fluffy inside
|
|
|
|
|
|
This is very true, and a good piece of advice
These logging functions will only ever be called if the application is configured to output "uber developer debug logging"
|
|
|
|
|
This is what I got
[Conditional(<FONT color=maroon>"TRACE"</FONT>)]
<FONT color=blue>public static void</FONT> WriteLine(<FONT color=blue>string</FONT> category, <FONT color=blue>string</FONT> format, <FONT color=blue>params object</FONT><FONT color=darkblue>[]</FONT> args)
{
<FONT color=blue>if</FONT> (debugmode)
{
<FONT color=darkgreen>
StackTrace st = <FONT color=blue>new</FONT> StackTrace(<FONT color=blue>false</FONT>);
StackFrame sf = st.GetFrame(<FONT color=red>2</FONT>);
<FONT color=blue>string</FONT> msg = <FONT color=blue>string</FONT>.Format(<FONT color=maroon>"{0,-15}:{1,-60}:{2}"</FONT>, category,
sf.GetMethod(), <FONT color=blue>string</FONT>.Format(format, args).Replace(<FONT color=maroon>"\n"</FONT>, <FONT color=maroon>"\\n"</FONT>).Replace(<FONT color=maroon>"\t"</FONT>,<FONT color=maroon>"\\t"</FONT>));
System.Diagnostics.Trace.WriteLine(msg);
}
<FONT color=blue>else</FONT>
{
<FONT color=blue>string</FONT> msg = category + <FONT color=maroon>" : "</FONT> + <FONT color=blue>string</FONT>.Format(format, args);
System.Diagnostics.Trace.WriteLine(msg);
}
}
xacc.ide-0.1.1 released! :) Download and screenshots
|
|
|
|
|
Dear all
I have executable that can execute more than once in the same time (you 'll have more than window of it in the same it )how can i change the setting to make it make hin execute once
thx for helping
Best Regards
Charbel
|
|
|
|
|
Where does the first link lead you?[^]
Cheers,
Vikram. "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
|
|
|
|
|
Where does the first link lead you?[^]
Cheers,
Vikram. "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
|
|
|
|
|
I really like your "Where does the first link lead you?" response much more than my boring old "Google is your friend" response... Can I use it?
|
|
|
|
|
I didn't know I'd started a fad I would prefer you, as a token of appreciation for the idea, giving me half your salary and your firstborn.
Of course, use it by all means!
Cheers,
Vikram. "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
|
|
|
|
|
Hi all,
I have a Page which contains 3 User Controls. (Navigation, Main Content, and Cart Preview)
How can I access a Page variable from these User Controls? Bearing in mind that multiple pages will need to include these User Controls.
For exmaple: I need them to all have access to a querystring.
Thanks,
Pete
|
|
|
|
|
Hi...
i want my GUI to look Kazaa-like...
i got my mainform , with Main Menu... and i got 4 or 5 forms..
i want the user to select a command from the main menu and then the requested form will be displayed at the same window as the mainmenu.! (Maybe some sort of container?)
currently , if the users selects one of the commands, a new window (not showed in the taskbar) is opened with the requested form.. but it's still 2 forms ( 1 mainmenu and 1 requested form) opened at the same time , and in the requested form , No Menu...
i hope that my explanation is good enough...
...Yosi..
|
|
|
|
|
have you tried using panels?
do you need separate forms? if not use panels.
try to set all your panels the same demenisons, then build them one on top the other. build your menu(main) to toggle between panel1.visible=true; or =false per each menuItem selection.
string Beautiful;
Beautiful = "ignorant";
label1.Text = "The world is full of " + Beautiful +" people.";
Why is common sense such an un-common comodity?
|
|
|
|
|
A function adds values in different combinations and returns the sum. Below is a list of values it uses. How do i extract the values that were added together? Example, say the function returns 10. Mentally i can extract the values 8 and 2, and figure out that 10 represents a "16bit" "WAV". How do i do the same in code?
int WAV = 8;
int 24bit = 4;
int 16bit = 2;
int 8bit = 1;
The above value list is hypothetical. The real numbers i am working with are like the ones below. They come from an open source project called libsndfile.
SF_FORMAT_WAV = 0x010000, // Microsoft WAV format (little endian):
SF_FORMAT_PCM_S8 = 0x0001, // Signed 8 bit data
SF_FORMAT_PCM_16 = 0x0002, // Signed 16 bit data
SF_FORMAT_PCM_24 = 0x0003, // Signed 24 bit data
SF_FORMAT_PCM_32 = 0x0004, // Signed 32 bit data
Happy Holidays
|
|
|
|
|
You can perform logical operators to the number you have. For example:
int Wav16bit = 10;
if ((Wav16bit & SF_FORMAT_WAV) != 0)
{
}
if ((Wav16bit & SF_FORMAT_PCM_16) != 0)
{
}
NOTE: There is only one ampersand.
What it does is look at the bits in the number and perform a logical and operation. So only where positions where the bits match will it be true
0001 & 0001 = 0001 // Both numbers match
0010 & 0001 = 0000 // Not matching bits
0100 & 0101 = 0100 // Only the second bit matches
There is another useful, similar, operator - the logical or operator and it is represented by a vertical bar | It says that if any bits are on on either side then that bit will be on in the result:
0001 | 0001 = 0001
0010 | 0001 = 0011
0100 | 0011 = 0111
Does this help?
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
-- modified at 5:03 Wednesday 21st December, 2005
|
|
|
|
|
In your last example you talked about | and used &
|
|
|
|
|
All fixed up now, thanks.
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
|
|
|
|
|
I think i got it now. I did not put this in my post, but i learned there are "mask" constants used to filter out values.
SF_FORMAT_TYPEMASK = 0x0FFF0000,
SF_FORMAT_SUBMASK = 0x0000FFFF,
By &-ing these with the format number i can get the main format type (WAV, AIFF, AU, etc) or the format subtype (16-bit, 24-bit, etc). From my experimenting |-ing seems to do the opposite of &-ing. It is kind of like adding and subtracting. I am sure as i code more on a low level i will get a better understanding.
|
|
|
|
|
I need this kind of classes:
struct_db mystruct = new struct_db();
public struct str1{
public string nome;
public string lunghezza;
public string tipo;
}
public class tab{
public string nometab;
public str1[] fields;
public string[] index;
public string[] procedure;
}
public class struct_db{
public int count;
public tab[] tabs;
public struct_db(){
tabs = new tab[100];
}
}
if set a value at struct_db.tabs[i].index i have an error because this array is not initialized... how i can initialize the class tab before using?
Thanx
|
|
|
|
|
peppepinna wrote: tabs = new tab[100];
This just reseves the space for the array - but does not initialise it. You need to do something like this on each element:
tabs[i] = new tab();
Does this help?
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
|
|
|
|
|
Yes i have to initialize for each elements of tabs[i] the struct tab that is not initialised, ad if i give a value at the elements of tab i have an error because this value is a "null reference", with the struct_db = new ... i reserve space for struct db and reserve the space for tabs, but i cannot reserve the space for the elements of tabs. I have try to put "tabs[i] = new tab()" into initialization of struct_db but it doesen't work.
Thanx
|
|
|
|
|
Many thanx you have give me an idea, i have solved my problem, i do a new tab() for each element into the struct_db(){...} and into my tab classes i initialize my arrays.
Many many thanx bye.
|
|
|
|