Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created January 19, 2011 02:01
Show Gist options
  • Select an option

  • Save mgroves/785545 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/785545 to your computer and use it in GitHub Desktop.
lazy loading 4
[Serializable]
public sealed class LazyLoadAttribute : LocationInterceptionAspect
{
private Type _type;
public override bool CompileTimeValidate(PostSharp.Reflection.LocationInfo locationInfo)
{
// DependencyMap.GetConcreteType will just return the concrete type given an interface
_type = DependencyMap.GetConcreteType(locationInfo.LocationType);
if(_type == null)
{
return false;
}
return true;
}
public override void OnGetValue(LocationInterceptionArgs args)
{
args.ProceedGetValue();
if (args.Value == null)
{
args.SetNewValue(Activator.CreateInstance(_type));
}
args.ProceedGetValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment