1

I'm working on a very legacy code, here coupling is on the GOD level. So somehow I need to get the name of the caller which called the property setter.

I've really invested my time to find a solution, I've tried stack frames also to get the name of the method. From this answer

For example

[System.Xml.Serialization.XmlElementAttribute("orOperatorAppendixed", Form = XmlSchemaForm.Qualified)]
        public bool OrOperatorAppendixed
        {
            get { return orOperatorAppendixed; }
            set
            {

                StackTrace stackTrace = new StackTrace();           // get call stack
                StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)

                // write call stack method names
                foreach (StackFrame stackFrame in stackFrames)
                {
                    Console.WriteLine(stackFrame.GetMethod().Name);   // write method name
                }
                orOperatorAppendixed = value;

            }
        }

When someone calls that setter, console messages looks like that

set_OrOperatorAppendixed
Read52_Condition
Read53_Alarm
Read77_Pip
Read152_Window
Read152_Window
Read159_Layout
Read160_layout
InvokeMethod
UnsafeInvokeInternal
Invoke
InvokeReader
Deserialize
Deserialize
FromXml
layoutReceived
processData
dataReceived
dataReceived
Complete
CompleteCallback
RunInternal
Run
Run
Complete
ProtectedInvokeCallback
CompletionPortCallback
PerformIOCompletionCallback

Basically, I think these method names are runtime level methods. So what I want to say is that I want the caller name of the function in my project, especially the method which was written by developers of that project which is calling that property setter.

Also, I've read about [CallerMemberName]string callername=" ", but you need to path CallerMemberName as a parameter and I have property, so I can't do it.

So I hope you understand what is my core problem. Maybe there is some way to solve my problem.

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tornike Gomareli
  • 1,564
  • 2
  • 16
  • 28

0 Answers0