I want to be able to restrict usage of a instance variable to one method, and other usages should not be possible (compile error, or warning). For instance
public class Example
{
public string Accessor()
{
if (SuperPrivate == null) // allowed
{
SuperPrivate = "test"; // allowed
}
return SuperPrivate; // allowed
}
private string SuperPrivate;
public void NotAllowed()
{
var b = SuperPrivate.Length; // access not allowed
SuperPrivate = "wrong"; // modification not allowed
}
public void Allowed()
{
var b = Accessor().Length; // allowed
// no setter necessary in this usecase
}
}
Usage of Lazy, automatic properties, or encapsulation in a separate object is not possible. I thought about extending ObsoleteAttribute, but it's sealed.