MetalamaConceptual documentationCreating aspectsAdvising a single type with a fabric
Open sandboxFocusImprove this doc

Advising a single type with a fabric

Instead of using aspects, you can advise the current type using a type fabric. A type fabric is a compile-time nested class that functions as a type-level aspect added to the target type.

To advise a type using a type fabric, follow these steps:

  1. Create a nested type derived from the TypeFabric class.

    Note

    For optimal design-time performance and usability, we recommend implementing type fabrics in a separate file and marking the containing type as partial.

  2. Override the AmendType method.

  3. Utilize the methods exposed on the amender.Advice property. To use this feature, you must be familiar with advanced aspects. For more details, refer to Transforming code.

  4. Optionally, you can add declarative advice, such as member introductions, to your type fabrics. For more information, see Introducing members.

Note

Type fabrics are always executed first, before any aspect. As a result, they can only add advice to members defined in the source code. If you need to add advice to members introduced by an aspect, you will need to use a helper aspect and order it after the aspects that provide the members you wish to advise.

Example

The following example demonstrates how to create a type fabric that introduces ten methods to the target type.

Source Code
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Fabrics;
3
4namespace Doc.AdvisingTypeFabric
5{
6    internal class MyClass
7    {



8        private class Fabric : TypeFabric
9        {



10            [Template]
11            public int MethodTemplate( [CompileTime] int index )
12            {
13                return index;
14            }






























15
16            public override void AmendType( ITypeAmender amender )
17            {
18                for ( var i = 0; i < 10; i++ )
19                {





20                    amender.Advice.IntroduceMethod(
21                        amender.Type,



22                        nameof(this.MethodTemplate),
23                        args: new { index = i },
24                        buildMethod: m => m.Name = "Method" + i );
25                }
26            }
27        }




28    }




29}
Transformed Code
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Fabrics;
3
4namespace Doc.AdvisingTypeFabric
5{
6
7#pragma warning disable CS0067, CS8618, CS0162, CS0169, CS0414, CA1822, CA1823, IDE0051, IDE0052
8
9    internal class MyClass
10    {
11
12#pragma warning disable CS0067, CS8618, CS0162, CS0169, CS0414, CA1822, CA1823, IDE0051, IDE0052
13
14        private class Fabric : TypeFabric
15        {
16            [Template]
17            public int MethodTemplate([CompileTime] int index) => throw new System.NotSupportedException("Compile-time-only code cannot be called at run-time.");
18
19
20            public override void AmendType(ITypeAmender amender) => throw new System.NotSupportedException("Compile-time-only code cannot be called at run-time.");
21        }
22
23#pragma warning restore CS0067, CS8618, CS0162, CS0169, CS0414, CA1822, CA1823, IDE0051, IDE0052
24
25
26
27        public int Method0()
28        {
29            return 0;
30        }
31
32        public int Method1()
33        {
34            return 1;
35        }
36
37        public int Method2()
38        {
39            return 2;
40        }
41
42        public int Method3()
43        {
44            return 3;
45        }
46
47        public int Method4()
48        {
49            return 4;
50        }
51
52        public int Method5()
53        {
54            return 5;
55        }
56
57        public int Method6()
58        {
59            return 6;
60        }
61
62        public int Method7()
63        {
64            return 7;
65        }
66




67        public int Method8()
68        {
69            return 8;
70        }
71
72        public int Method9()
73        {
74            return 9;
75        }
76    }
77
78#pragma warning restore CS0067, CS8618, CS0162, CS0169, CS0414, CA1822, CA1823, IDE0051, IDE0052
79
80
81}