I don't think there is a language construct in c# to do this. I see two approaches:
1) Pass caller to the methods, and check type in the method.
A) Separating methods
public class Debt
{
private bool _IsAccountLocked;
public void setIsAccountLocked(object caller, bool value)
{
if(!caller is UserAccounts)
{
throw new Exception("Only a UserAccount instance can set this");
}
_IsAccountLocked = value;
}
public bool IsAccountLocked
{
get { return _IsAccountLocked; }
}
}
B) Indexed property (khmmm...)
public class Debt
{
private bool _IsAccountLocked;
public bool IsAccountLocked[object caller]
{
get { return _IsAccountLocked; }
set {
if(!caller is UserAccounts)
{
throw new Exception("Only a UserAccount instance can set this");
}
_IsAccountLocked = value;
}
}
}
In both cases, the caller should pass
this
for the
caller parameter.
2) If you can't trust the caller, you can get the calling class from the stack:
http://www.techdreams.org/microsoft/c-how-to-get-calling-class-name-method-name-using-stackframe-class/5815-20110507[
^]