[Home](../../) › [Metalama](../../metalama) › [Features](../features.md) › [Core Features](core.md) › [Code Generation](code-generation.md) › Enhance Hand-Written Code

# Enhance Hand-Written Code

> Metalama adds behaviors to existing C# code, enabling features like logging and exception handling without altering the original methods in your source.

Canonical: https://postsharp.net/metalama/features/override.html

Metalama is the only Roslyn-based C# generator that lets you add behaviors to existing hand-written code. While other source generators allow you to add new classes to a project or extend partial classes, they don't let you inject new behaviors into existing methods, properties, or fields.

## Benefits

* Features like instrumentation (logging, metrics), exception handling (retry, throttling), observability (`INotifyPropertyChanged`), and thread synchronization (locking) all require modifying existing code; adding new classes is not enough.
* It's transparent. There is no need for the `partial` keyword.

## Example

Suppose you have this source code:

```cs
public class HatShop
{
    public void PlaceOrder()
    {
        Console.WriteLine( "Ordering a hat." );
    }
}
```

We want to measure the number of executions of all public methods in our project, so we add this fabric:

```csharp
internal class Fabric : ProjectFabric
{
    public override void AmendProject(IProjectAmender amender) =>
        amender
            .SelectTypes()
            .Where(type => type.Accessibility == Accessibility.Public)
            .SelectMany(type => type.Methods)
            .Where(method => method.Accessibility == Accessibility.Public)
            .AddAspectIfEligible<MeasureExecutionCountAttribute>();
}
```

The `[MeasureExecutionCount]` attribute will transform the `HatShop` class into this:

```csharp
public class HatShop
{
  // This field is added by the aspect.
  private HatShopMetrics _hatShopMetrics;

  // This constructor is added by the aspect. It pulls the dependency.
  public HatShop(HatShopMetrics hatShopMetrics = null)
  {
    this._hatShopMetrics = hatShopMetrics;
  }

  public void PlaceOrder()
  {
    // This line of code is added by the aspect.
    this._hatShopMetrics?.PlaceOrderExecutionCount.Add(1);

    // This is the original source code.
    Console.WriteLine("Ordering a hat.");
  }
}
```

As you can see, it adds new members like the `HatShopMetrics` class and field, and it also injects logic into the `PlaceOrder` method.

## Resources

* Reference documentation: [Creating simple aspects](https://doc.metalama.net/conceptual/aspects/simple-aspects)

---

## Site navigation

- Previous: [C#-to-C# Templates](templates.md)
- Next: [Reference Generated Code](reference-from-source-code.md)
- Index of the whole site: [llms.txt](../../llms.txt)

