-1

I have a property called MyProperty. I am interested in getting a reference to the object that called the setter of the property. For example:

MyProperty
{
  set
  {
    if (modifer.GetType() == typeof(UIControl))
    {
      //some code
    }
    else
    {
      //some code
    }
  }
}
Ben Reich
  • 16,222
  • 2
  • 38
  • 59

4 Answers4

0

I don't think that's possible unless you unwind the stack? Which you could do but I wouldn't recommend it.

Code Junkie
  • 519
  • 2
  • 7
  • 16
0

You could make use of reflection. Though this approach is not something I'd recommend.

StackTrace stackTrace = new StackTrace();
var modifier = stackTrace.GetFrame(1).GetType();

if (modifier == typeof(UIControl))
{
  //some code
}

I didn't test this, but should be about right.

You could also check out CallerMemberNameAttribute, it might be relevant to you.

Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
0

It is possible to examine the current stack during run time. This would enable you to check the class of the calling method:

StackTrace stackTrace = new StackTrace();
var callingMethod = stackTrace.GetFrame(1).GetMethod();
var callingType = callingMethod.ReflectedType 
//Or possible callingMethod.DeclaringType, depending on need

However, this type of code should set of alarms. Using reflection to unwind the stack is fragile, unintuitive, and defies separation of concerns. The setter of a property is an abstraction intended to set the value given nothing other than the value to set.

There are several stronger alternatives. Among them, consider having a method called only by UIControls:

public void SetMyPropertyFromUiControl(MyType value)
{
    this.MyProperty = value;
    ... other logic concerning UIControl
}

If the details of the instance of UIControl being used to set the property are important, the method signature might look like:

public void SetMyPropertyFromUiControl(MyType value, UIControl control)
{
    this.MyProperty = value;
    ... other logic concerning UIControl that uses the control parameter
}
Ben Reich
  • 16,222
  • 2
  • 38
  • 59
  • The reflecion method will not work if the call to the property was inlined. In this case you can get the caller of your caller method and type back. But it will work perfectly inside the debugger because JIT optimizations are turned off usually while debugging. – Alois Kraus Jun 23 '13 at 22:15
  • @AloisKraus Good point! Another reason such an approach is weak. – Ben Reich Jun 23 '13 at 22:16
0

Actually there is a new feature in .NET 4.5, which is called "Caller Information".

You can get some information about caller like that:

public Employee([CallerMemberName]string sourceMemberName = "", 
                [CallerFilePath]string sourceFilePath = "", 
                [CallerLineNumber]int sourceLineNo = 0)
{
    Debug.WriteLine("Member Name : " + sourceMemberName);
    Debug.WriteLine("File Name : " + sourceFilePath);
    Debug.WriteLine("Line No. : " + sourceLineNo);
}


More information: Caller Info - codeguru.com

Yogu
  • 9,165
  • 5
  • 37
  • 58
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67