Open sandboxFocus

Class ProjectLocal<T>

Stores a value that belongs to the project that PostSharp is currently processing. Use it instead of a static field to hold build-time state, for instance in an aspect or in a build-time service.

Inheritance
ProjectLocal<T>
Namespace: PostSharp.Extensibility
Assembly: PostSharp.dll
Syntax
public sealed class ProjectLocal<T>
Type Parameters
Name Description
T

Type of the value.

Remarks

Store the ProjectLocal<T> itself in a static field. Each project then has its own value, even when PostSharp processes several projects in the same process at the same time. A static field would share its value between these projects.

Each project starts with the value that the initial value factory returns, or with the default value of T. The factory runs at most once per project, even when several threads of the project read the value for the first time at the same time.

A value exists only while PostSharp processes a project. At run time, and at build time outside of the processing of a project, Value throws an InvalidOperationException. Use TryGetValue(out T) in code that also runs at run time.

The value of the current project flows to the tasks and threads that the processing of the project starts.

Examples

private static readonly ProjectLocal<HashSet<MethodInfo>> processedMethods =
new ProjectLocal<HashSet<MethodInfo>>( () => new HashSet<MethodInfo>() );

// At build time: processedMethods.Value.Add( method );

Constructors

Name Description
ProjectLocal(Func<T>)

Initializes a new instance of the ProjectLocal<T> class.

ProjectLocal(string, Func<T>)

Initializes a new instance of the ProjectLocal<T> class, whose storage is identified by a name.

Properties

Name Description
SlotName

Gets the name that identifies the storage of this value in the process, or null when the ProjectLocal<T> was created without a name.

Value

Gets a reference to the value of the current project.

Methods

Name Description
TryGetValue(out T)

Gets the value of the current project, if PostSharp is processing a project.

See Also