S
sklett
This might be a weird one ;0)
I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.
I'd like to know if there is ANY way to restrict access to a field. I found
a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;
protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}
public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>
Of course if I'm just careful and don't access the field directly everything
will be fine, but I need to do this is many classes and it's my instinct to
access fields directly if I can.
One solution would be to put the lazy loaded stuff in a base class, but this
isn't ideal and wouldn't make much sense to anyone looking at the code.
If anyone knows of any obscure attributes that might help me accomplish this
I would really appreciate it.
Thanks,
Steve
I want to lazy load some member fields in a class. I had planned on
handling this in the Property for the field, think; if(field == null) //
load field data.
I'd like to know if there is ANY way to restrict access to a field. I found
a code sample that looked like it *might* do what I want (NOTE the
AccessedThroughProperty attribute):
<code>
class SomeClass
{
[AccessedThroughProperty("GlobalSettings")]
private string _globalSettings = null;
protected string GlobalSettings
{
get
{
if(_globalSettings == null)
{
// load data
}
return _globalSettings;
}
}
public void SomeMethod()
{
string s = _globalSettings; // I would LOVE it if this would
throw a compiler error
}
}
</code>
Of course if I'm just careful and don't access the field directly everything
will be fine, but I need to do this is many classes and it's my instinct to
access fields directly if I can.
One solution would be to put the lazy loaded stuff in a base class, but this
isn't ideal and wouldn't make much sense to anyone looking at the code.
If anyone knows of any obscure attributes that might help me accomplish this
I would really appreciate it.
Thanks,
Steve