Open sandboxFocusImprove this doc

Automatically trimming, upper-casing or normalizing strings

This project contains very simple aspects that implement several cases of string normalization:

  • Trimming,
  • Upper-casing,
  • UTF normalization.

They transform the code as follows:

Source Code



1namespace Metalama.Samples.NormalizeStrings;
2
3public class Class1
4{


5    // Applied on a property.
6    [Trim]
7    public string FirstName { get; set; } = "Borek";

8
9    // Applied on a field.
10    [Normalize]
11    public string LastName = "Stavitel";











12
13    // Applied on a parameter - nullable.
14    public void SetCountry( [ToUpperCase] string? country )
15    {
16        Console.WriteLine( $"Country: {country}" );
17    }
18}
Transformed Code
1using System;
2using System.Globalization;
3
4namespace Metalama.Samples.NormalizeStrings;
5
6public class Class1
7{
8private string _firstName = "Borek";
9
10    // Applied on a property.
11    [Trim]
12    public string FirstName { get { return this._firstName; } set { value = value.Trim(); this._firstName = value; } }
13    private string _lastName = "Stavitel";
14

15    [Normalize]
16    public string LastName
17    {
18        get
19        {
20            return this._lastName;
21        }
22        set
23        {
24            value = value.Normalize();
25            this._lastName = value;
26        }
27    }
28
29    // Applied on a parameter - nullable.
30    public void SetCountry( [ToUpperCase] string? country )
31{country = country?.ToUpper(CultureInfo.CurrentCulture);
32        Console.WriteLine( $"Country: {country}" );
33}}

Base aspect class

We created a base class that shares the common behaviors for all aspects:

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using Metalama.Framework.Eligibility;
4
5namespace Metalama.Samples.NormalizeStrings;
6
7public abstract class StringContractAspect : ContractAspect
8{
9    public override void BuildEligibility( IEligibilityBuilder<IFieldOrPropertyOrIndexer> builder )
10        => builder.Type().MustEqual( typeof(string) );
11
12    public override void BuildEligibility( IEligibilityBuilder<IParameter> builder )
13        => builder.Type().MustEqual( typeof(string) );
14
15    [CompileTime]
16    protected static bool IsAppliedToNullableString()
17    {
18        var type = meta.Target.Declaration switch
19        {
20            IFieldOrProperty fieldOrProperty => fieldOrProperty.Type,
21            IParameter parameter => parameter.Type,
22            _ => throw new InvalidOperationException()
23        };
24
25        return type.IsNullable == true;
26    }
27}

This class derives from ContractAspect because this base class conveniently supports fields, properties and parameters at the same type. As its name does not suggest, this class can be used to modify the value, not just to validate it.

The StringContractAspect class overrides BuildEligibility to limit the eligibility of the aspects to fields, properties and parameters of type string. For details regarding eligibility, see Defining the eligibility of aspects.

It also defines a helper method to determine whether the field, property or parameter is nullable.

Concrete aspect classes

With this base class, the concrete implementations are almost trivial. The only difficulty is that the nullable and non-nullable cases must be handled separately.

Let's look at the implementation of the Trim aspect:

1using Metalama.Framework.Aspects;
2
3namespace Metalama.Samples.NormalizeStrings;
4
5[RunTimeOrCompileTime]
6public sealed class TrimAttribute : StringContractAspect
7{
8    public override void Validate( dynamic? value )
9    {
10        if ( IsAppliedToNullableString() )
11        {
12            value = ((string?) value)?.Trim();
13        }
14        else
15        {
16            value = ((string) value!).Trim();
17        }
18    }
19}