I'm beginning to look into custom attributes, and I've come up with the following idea: what if I could make an attribute which would restrict the use of a variable to the property it backed?
[RestrictToProperty("Foo")]
private object _foo;
public object Foo
{
get { return _foo; }
set
{
_foo = value;
OnFooChanged(EventArgs.Empty);
}
}
public object NotFoo
{
get { return _foo; } // Warning
set { _foo = value; } // Warning
}
public void Bar()
{
_foo = new object(); // Warning
}
// Warning: 'MyClass._foo' should not be used outside of property 'Foo'
I believe it's possible, because Obsolete
does a similar thing.
[Obsolete]
private object _foo;
public void Bar()
{
_foo = new object(); // Warning: 'MyClass._foo' is obsolete
}
Unfortunately, I have no idea how to go about it, and can't find much beyond simple runtime attribute tutorials. Is this possible? If so, where would I start?