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
}