-
I've got a project on which I'd like to introduce a class with a method that the developer can use at development-time. The aspect appears to work great, but I'm running into an issue using it alongside a fabric. Usually, in the fabric, I'd select all the available types and narrow down to those that I want to apply the aspect to. Here though, I simply want to introduce the class to the project, ideally to whatever class contains the entry point (so I can repurpose its namespace so my introduced class is discoverable), but I can't figure out how to set up the fabric to discover this, nor really narrow down the fabric to any single type (e.g. there's no First method available) without specifying a name of it. The best I've come up with is naming amender.SelectMany(c => c.Types)
.Where(t => t is {TypeKind: TypeKind.Class, Name: "Program"})
.AddAspectIfEligible(//... ); This works, but is there a better approach I should take instead? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
If you want to introduce a top-level class (as opposed to a nested class), you would typically have a compilation-level aspect ( |
Beta Was this translation helpful? Give feedback.
-
As for the namespace, you could use the Then you can do e.g.: class Fabric : ProjectFabric
{
public override void AmendProject(IProjectAmender amender)
{
amender.AddAspect(c => new YourCompilationAspect(c.Project.TryGetProperty("RootNamespace", out var ns) ? ns : null));
}
} |
Beta Was this translation helpful? Give feedback.
-
Excellent, thank you. |
Beta Was this translation helpful? Give feedback.
As for the namespace, you could use the
RootNamespace
MSBuild property. It's probably already exposed to the compiler/Metalama in your project (if not, add<CompilerVisibleProperty Include="RootNamespace"/>
inside an<ItemGroup>
in your project file).Then you can do e.g.: