C# has no pattern keyword.
Metalama fixes that.
Metalama is an open-source patterns & architecture toolkit for C#.
Define your team's patterns once: the compiler writes the repetitive parts at build time and enforces your rules as you type.
- Write the pattern once, apply it everywhere. Aspects generate logging, caching,
INotifyPropertyChanged, or your own patterns at compile time. The boilerplate never lands in your repo, so it never needs review or maintenance. - Enforce architecture as you type. Express dependency rules, naming conventions, and pattern guidelines in plain C# and get real-time feedback in the IDE, long before the pull request.
- Stay consistent in the AI era. Hand-written or AI-generated, every line is checked against your rules. When a pattern changes, you edit one file and the whole codebase follows at the next build.
Built by the makers of PostSharp 20+ years of meta-programming in .NET
Latest Project Updates
What's the missing pattern keyword costing you?
Boilerplate & Architecture Erosion
When a pattern lives only in your head, you pay twice: you expand it by hand in a thousand places, and nothing stops the codebase from drifting away from it.
A Total Waste of Time 
Manually writing boilerplate is boring, frustrating, and a total waste of your time. Why do what a machine can do faster?
Avoidable Defects 
Boilerplate is riddled with bugs: it's copy-pasted and largely untested. Quality and performance suffer.
Rules Nobody Enforces 
Your architecture lives in a wiki. The compiler has never read it, and neither has the new hire.
Death by a Thousand Commits 
Every shortcut that passes review moves the code a little further from the design, until small changes are expensive and refactoring is unfeasible.
Show Me Code!
How Does It Work?
1. Create an aspect
Aspects are meta-classes that transform code using simple C# templates. This one wraps a method in logging (entry, return value, and any exception) using a real try/catch.
public class LogAttribute : OverrideMethodAspect { public override dynamic? OverrideMethod() { Console.WriteLine($"Entering {}"); try { var result = (); Console.WriteLine($"{} returned {result}."); return result; } catch (Exception e) { Console.WriteLine($"{} failed: {e.GetType().Name}"); throw; } } }
2. Apply it to business code
Add the aspect wherever you need it. Drop the [Log] attribute on a single method, or select hundreds at once with a fabric: compile-time LINQ-like code that runs over your project.
public class OrderService { [Log] public Order PlaceOrder(Cart cart) => _repository.Save(cart); }
public class LoggingFabric : ProjectFabric { public override void AmendProject(IProjectAmender amender) { amender.SelectTypes() .Where(t => t.Accessibility == Accessibility.Public) .SelectMany(t => t.Methods) .Where(m => m.Accessibility == Accessibility.Public) .AddAspectIfEligible<LogAttribute>(); } }
3. The result
At compile time, Metalama weaves the aspect into every matching method. Here is PlaceOrder as it ships. The green lines are generated; your original code stays untouched.
public Order PlaceOrder(Cart cart) { Console.WriteLine("Entering OrderService.PlaceOrder(Cart)"); try { var result = _repository.Save(cart); Console.WriteLine($"OrderService.PlaceOrder(Cart) returned {result}."); return result; } catch (Exception e) { Console.WriteLine($"OrderService.PlaceOrder(Cart) failed: {e.GetType().Name}"); throw; } }
How Does It Compare?
| Metalama | PostSharp | Roslyn Plug-In | Fody | |
|---|---|---|---|---|
| Transformation of the compilation using low-level APIs | Yes | Yes | No | Yes |
| Addition of behaviors to source code using simple custom aspects or templates | Yes | Yes | No | No |
| Introduction of new members or interfaces and referencing them in the source code | Yes | No | Complex | No |
| Analysis of the source code and reporting of warnings and errors in real-time | Yes | Requires Rebuild | Complex | Complex / Requires Rebuild |
| Debugging and exporting of transformed code | Yes | No | N/A | No |
What's Holding You Back?
You might be considering using directly Roslyn's low-level APIs such as analyzers, generators and interceptors.
Not only does Metalama provide a simpler and more integrated solution on the top of Roslyn, but it also adds features Roslyn simply don't offer.
Metalama is Simpler. Meta-programming at the abstraction level of Roslyn is intricate, demanding years of experience. When approached naively, low-level meta-programming can resemble hacking and can significantly amplify complexity, especially after the departure of the developer who implemented the meta-code.
Metalama is Integrated. Metalama stands alone as the only tool that allows you to both generate code visible at design time and override hand-written code. To replicate what Metalama offers, you would need to juggle a combination of Roslyn APIs that lack integration.
Metalama is Engineered for Good Architecture. Metalama is designed to simplify development. It avoids offering hacks that render the code less predictable or comprehensible. Software development encompasses three cultures: hacking, science, and engineering. We identify with the last one while remaining mindful of the other two.
Metalama is a Complete, Well-thought-out Solution. Unlike alternatives that focus on the most common use cases and cut corners (for instance, by failing to properly implement async methods), Metalama incorporates enough extension points to ensure you never hit a dead end.
Delegation is what you actually want. You want the machine to handle low-level, repetitive details, freeing up more of your time to focus on meaningful tasks.
You're already accustomed to This. Initially, people were apprehensive about losing control over performance when the industry transitioned from assembly language to C, and then to the managed, garbage-collected memory model of C# and Java. We've relinquished control over low-level details but have gained enormous productivity and reliability.
You're not completely losing control. Metalama offers numerous mechanisms to override default behaviors.
You can revert to the source code at any time. There is no vendor lock-in.
You can and should use both. We are also fans of refactoring tools.
Metalama reduces the amount of code. While refactoring tools help developers write code faster, they do not reduce the amount of code necessary to implement a feature.
Metalama simplifies the writing of custom rules and code fixes. Other refactoring tools often have more complex APIs.
While meta-programming and AOP can introduce new concepts, Metalama is designed for a gradual learning curve.
Start simply, grow gently. Metalama's simple APIs can get you started in minutes. As you start understanding the technology and want to implement more complex rules, between 4 and 16 hours of learning might be necessary to master the framework.
Leverage ready-to-use aspects. Pull from the dozens of supported, open-source aspects that cover the most common requirements. Find them in Metalama Marketplace.
Comprehensive documentation. It offers clear documentation, tutorials, and API references to help developers onboard quickly.
Team Learning. Not everyone on the team needs to learn Metalama in detail. Only architects or senior developers writing aspects will need a detailed understanding. For other team members only using aspects, a simple 1-hour training will be enough to get started.
One of the key concerns with meta-programming is debugging, but Metalama provides tools to mitigate this.
Source Mapping. You can switch between source and transformed code debugging.
Code Preview. You can compare transformed code to source code before compilation, improving transparency thanks to Visual Studio Tools for Metalama.
Not at all. Metalama is a highly-optimized solution based on compile-time code inlining.
Compile-Time. Unlike runtime-based frameworks, Metalama does not rely on reflection or emitting dynamic proxies at run time.
Efficient. The transformations generate high-performance code that integrates seamlessly with the application. Unlike previous generations of AOP frameworks, Metalama does relies on code inlining (i.e. template expansion) and does not introduce performance or memory allocation overheads.
We believe that you will comprehend the business meaning of your code even better than before since it will not be cluttered with boilerplate. When the time comes to read the fine print, Metalama shows you the exact generated code.
Show or Debug Generated Code. Switch to the LamaDebug build configuration to see exactly the code generated by Metalama. You can even debug it!
Diff Preview. Compare source and transformed code directly within Visual Studio.
CodeLens Integration. See which aspects are applied to a method directly in the editor.
Aspect Explorer. See which aspects are present in the solution and how they affect the code.
Read more about Visual Studio tooling.
Metalama is built on Roslyn and is designed to work seamlessly with .NET development environments.
At design time, all editors that support Roslyn automatically support Metalama.
At build time, Metalama integrates with MSBuild and can be used in any build server.
Visual Studio Tools for Metalama are designed to enhance developers productivity but are not required for development.
Metalama is actively developed and maintained by PostSharp Technologies.
Established Vendor. PostSharp Technlogies has a 20-year long history of supporting AOP in .NET, providing confidence in long-term support.
Fallback Strategy. Since Metalama generates regular C# code, applications can transition to manual implementations if necessary thanks to the Metalama Divorce feature.
Like any tool, Metalama should be used wisely to maintain a clear and maintainable codebase.
We recommend:
- to use Metalama to automate patterns that affect at least 20 classes or properties;
- to avoid hacks and to preserve the semantical and conceptual integrity of the code (don't do anything unexpected);
- to defensively validate the code to which aspects are applied, and reporting errors upon unsupported situations instead of generating code that might not compile.
Metalama is designed for scalable projects but can be used selectively where it adds real value.
Progressive Adoption. Developers can start with simple aspects and expand usage as needed.
Pragmatic Approach. We advise against overuse and promote using aspects only where beneficial.