Search code examples
.netpostsharp

PostSharp: Identify project type of assembly being woven?


Is there a good way to identify the project output type of the target assembly at weaving time? I'm hoping to make an IAspectProvider that applies different aspects depending on whether the target is:

  • ASP.NET project (Web Site, Web Application)
  • Windows Service
  • Desktop/Console Application
  • Class Library

Edit: I have accepted an answer as it has identified that there is no built-in mechanism in PostSharp for identifying the type of project being woven; it will either need to be manually specified or obtained in a different way.


Solution

  • One possibility is to pass this information to PostSharp through MSBuild. For example, you can add this to your csproj:

    <PostSharpProperties>$(PostSharpProperties);ProjectType=Console</PostSharpProperties>
    

    In compile time, you can read the property ProjectType this way:

    PostSharpEnvironment.CurrentProject.EvaluateExpression("{$ProjectType}")
    

    Docs:

    This is how to pass this information to PostSharp. Another question is how to obtain this information if you wouldn't like hard-code it to the project file manually.