PostSharp 4.3 / / Post­Sharp Documentation / Deployment and Configuration / Configuration / Accessing Configuration from Source Code
Accessing Configuration from Source Code

Even if most configuration settings are consumed by PostSharp or its add-in, it is sometimes useful to access configuration elements from user code. The PostSharp.dll library gives access to both configuration properties and extension configuration elements.

This topic contains the following sections:

Accessing properties

You can read the value of any PostSharp property by including it in an XPath expression and evaluating it using the EvaluateExpression(String) method of PostSharpEnvironment.CurrentProject:

C#
string
                         
                        value
                         = PostSharpEnvironment.CurrentProject.EvaluateExpression(
                        "{$PropertyName}"
                        )
                      

For details regarding expressions, see Using Expressions in Configuration Files.

Accessing custom sections

You can get a list of custom sections of a given name and namespace by calling the GetExtensionElements(String, String) method of PostSharpEnvironment.CurrentProject:

C#
IEnumerable<ProjectExtensionElement> elements = 
     PostSharpEnvironment.CurrentProject.GetExtensionElements( 
                        "MyElement"
                        , 
                        "uri:MyNamespace"
                         );
                      

Extension elements must be declared using the SectionType element.

See Also