In this chapter, I'll show you my AOP framework - Teddy's Aspect Weaver which is based on AspectDNG's ILML library. I just do my best to make this framework easier for using and avoid AspectDNG's disadvantages and encumbering facts. Also this framework extends functions of AspectDNG and provides more supported types of advices. Let's take a closer look with the example within the Aspect Weaver V0.6 source code and sample.
Return to Contents
7. Build a More Powerful AOP Framework Based on AspectDNG's ILML Library - Teddy's Aspect Weaver.
In this chapter, I'll show you my AOP framework - Teddy's Aspect Weaver which is based on AspectDNG's ILML library. I just do my best to make this framework easier for using and avoid AspectDNG's disadvantages and encumbering facts. Also this framework extends functions of AspectDNG and provides more supported types of advices. Let's take a closer look with the example within the Aspect Weaver V0.1 source code.
7.1 Command Line Usage
AspectWeaver.exe Path_to_AspectWeaverConfiguration.xml
7.2 Configuration Format
The full configuration format is defined in AspectWeaver/AspectWeaver.xsd with comments for each Advice's specific configuration. And the best way to learn how to use is to work through the example.
Full TestAspectConfiguration.xml source code
<?xml version="1.0" encoding="utf-8" ?>
<Configuration logFile="LogWeaving.xml" cleanTempFiles="false">
<BaseAssembly>..\TestLib\bin\Debug\TestLib.dll</BaseAssembly>
<OutputAssembly>..\TestLib\bin\Debug\TestLib_output.dll</OutputAssembly>
<AspectAssemblies>
<AspectAssembly uniqueName="TestAspectLib.dll" path="bin\Debug\TestAspectLib.dll" />
</AspectAssemblies>
<AdviceFiles>
<AdviceFile>Advice-Test.xml</AdviceFile>
</AdviceFiles>
</Configuration>
Full Advice-Test.xml source code
<?xml version="1.0" encoding="utf-8" ?>
<Advices>
<Delete>
<Pointcut targetXPath="//Type[@name='ClassToBeDeleted']" />
</Delete>
<Delete>
<Pointcut targetXPath="//Field[@name='fieldToBeDeleted']" />
</Delete>
<Delete>
<Pointcut targetXPath="//Method[@name='MethodToBeDeleted']" />
</Delete>
<Introduce>
<Pointcut targetXPath="//Module" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Type[@name='ClassToBeIntroduced']"></Code>
</Introduce>
<Introduce>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Field[@name='fieldToBeIntroduced']"></Code>
</Introduce>
<Introduce>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='MethodToBeIntroduced']"></Code>
</Introduce>
<SetBaseType>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<BaseType>System.MarshalByRefObject</BaseType>
</SetBaseType>
<ImplementInterface>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<InterfaceType>TestLib.InterfaceToBeImpelementInterface</InterfaceType>
</ImplementInterface>
<MakeSealed>
<Pointcut targetXPath="//Type[@name='TestClass']" />
</MakeSealed>
<MakeSerializable>
<Pointcut targetXPath="//Type[@name='TestClass']" />
</MakeSerializable>

<BeforeConstructorCall>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeBeforeConstructorCall']"></Code>
</BeforeConstructorCall>
<AfterConstructorCall>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAfterConstructorCall']"></Code>
</AfterConstructorCall>
<BeforeMethodCall>
<Pointcut targetXPath="//Method[@name='MethodUsedToTestMethodBeforeAfterCall']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeBeforeMethodCall']"></Code>
</BeforeMethodCall>
<AfterMethodCall>
<Pointcut targetXPath="//Method[@name='MethodUsedToTestMethodBeforeAfterCall']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAfterMethodCall']"></Code>
</AfterMethodCall>
<InlineAtStart>
<Pointcut targetXPath="//Method[@name='TestInlineAtStartBeforeReturn']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineAtStart']"></Code>
</InlineAtStart>
<InlineAtStart>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineAtStart']"></Code>
</InlineAtStart>
<InlineBeforeReturn>
<Pointcut targetXPath="//Method[@name='TestInlineAtStartBeforeReturn']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineBeforeReturn']"></Code>
</InlineBeforeReturn>
<InlineBeforeReturn>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineBeforeReturn']"></Code>
</InlineBeforeReturn>
<AroundBody>
<Pointcut targetXPath="//Method[@name='MethodToBeAroundedBody']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAroundedMethodBody']"></Code>
</AroundBody>
<AroundBody>
<Pointcut targetXPath="//Method[@name='set_PropertyToBeAroundedBody']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAroundedMethodBody']"></Code>
</AroundBody>
</Advices>
7.3 Advices Test Result
7.3.1 Delete
Only Type, Field or Method can be deleted. Below is the sample configuration.
<Delete>
<Pointcut targetXPath="//Type[@name='ClassToBeDeleted']" />
</Delete>
<Delete>
<Pointcut targetXPath="//Field[@name='fieldToBeDeleted']" />
</Delete>
<Delete>
<Pointcut targetXPath="//Method[@name='MethodToBeDeleted']" />
</Delete>
After weaving, the specific elements will be deleted from the base assembly.
7.3.2 Introduce
Introduce here means "Insert", we can insert Type, Field or Method from aspect assembly to base assembly. Below is the sample configuration.
<Introduce>
<Pointcut targetXPath="//Module" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Type[@name='ClassToBeIntroduced']"></Code>
</Introduce>
<Introduce>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Field[@name='fieldToBeIntroduced']"></Code>
</Introduce>
<Introduce>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='MethodToBeIntroduced']"></Code>
</Introduce>
7.3.3 SetBaseType, ImplementInterface, MakeSealed, MakeUnsealed and MakeSerializable
These advices are some degree simpler so I put them as a example together.
Configuration
<SetBaseType>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<BaseType>System.MarshalByRefObject</BaseType>
</SetBaseType>
<ImplementInterface>
<Pointcut targetXPath="//Type[@name='TestClass']" />
<InterfaceType>TestLib.InterfaceToBeImpelementInterface</InterfaceType>
</ImplementInterface>
<MakeSealed>
<Pointcut targetXPath="//Type[@name='TestClass']" />
</MakeSealed>
<MakeSerializable>
<Pointcut targetXPath="//Type[@name='TestClass']" />
</MakeSerializable>
Code before weaving
public class TestClass

{
}
Code after weaving (decompiled)
[SerializableAttribute()]
public sealed class TestClass : MarshalByRefObject, InterfaceToBeImpelementInterface

{
}
7.3.4 BeforeConstructorCall, AfterConstructorCall
These two advices insert code before/after where the constructors of specific Type be called.
Configuration
<BeforeConstructorCall>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeBeforeConstructorCall']"></Code>
</BeforeConstructorCall>
<AfterConstructorCall>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAfterConstructorCall']"></Code>
</AfterConstructorCall>
Code before weaving
public class TestClass

{
public TestClass()

{
}

public TestClass(object obj)

{
}

public void TestBeforeAfterConstructorCall()

{
object obj1 = new TestClass();
new TestClass("test");
}
}
public class TestAspectClass

{
public void CodeBeforeConstructorCall()

{
Console.Write("CodeBeforeConstructorCall");
}

public void CodeAfterConstructorCall()

{
Console.Write("CodeAfterConstructorCall");
}
}Code after weaving (decompiled)
public class TestClass

{
public TestClass()

{
}

public TestClass(object )

{
}

public void TestBeforeAfterConstructorCall()

{
Console.Write("CodeBeforeConstructorCall");
object local = new TestClass();
Console.Write("CodeAfterConstructorCall");
Console.Write("CodeBeforeConstructorCall");
new TestClass("test");
Console.Write("CodeAfterConstructorCall");
}
}
7.3.5 BeforeMethodCall and AfterMethodCall
These two advices are similar to the ConstructorCall ones but for MethodCall.
Configuration
<BeforeMethodCall>
<Pointcut targetXPath="//Method[@name='MethodUsedToTestMethodBeforeAfterCall']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeBeforeMethodCall']"></Code>
</BeforeMethodCall>
<AfterMethodCall>
<Pointcut targetXPath="//Method[@name='MethodUsedToTestMethodBeforeAfterCall']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAfterMethodCall']"></Code>
</AfterMethodCall>
Code before weaving
public class TestClass

{
public int MethodUsedToTestMethodBeforeAfterCall()

{
return 0;
}

public void TestBeforeAfterMethodCall()

{
int i = MethodUsedToTestMethodBeforeAfterCall();
MethodUsedToTestMethodBeforeAfterCall();
}
}
public class TestAspectClass

{
public void CodeBeforeMethodCall()

{
Console.Write("CodeBeforeMethodCall");
}

public void CodeAfterMethodCall()

{
Console.Write("CodeAfterMethodCall");
}
}
Code after weaving (decompiled)
public class TestClass

{
public int MethodUsedToTestMethodBeforeAfterCall()

{
return 0;
}

public void TestBeforeAfterMethodCall()

{
Console.Write("CodeBeforeMethodCall");
Console.Write("CodeAfterMethodCall");
int i = MethodUsedToTestMethodBeforeAfterCall();
Console.Write("CodeBeforeMethodCall");
MethodUsedToTestMethodBeforeAfterCall();
Console.Write("CodeAfterMethodCall");
}
}
7.3.6 InlineAtStart and InlineBeforeEnd
These advices insert code in to constructor or methods' body.
Configuration
<InlineAtStart>
<Pointcut targetXPath="//Method[@name='TestInlineAtStartBeforeReturn']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineAtStart']"></Code>
</InlineAtStart>
<InlineAtStart>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineAtStart']"></Code>
</InlineAtStart>
<InlineBeforeReturn>
<Pointcut targetXPath="//Method[@name='TestInlineAtStartBeforeReturn']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineBeforeReturn']"></Code>
</InlineBeforeReturn>
<InlineBeforeReturn>
<Pointcut targetXPath="//Type[@name='TestClass']/Constructor" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeInlineBeforeReturn']"></Code>
</InlineBeforeReturn>
Code before weaving
public class TestClass

{
public TestClass()

{
}

public TestClass(object obj)

{
}

public void TestInlineAtStartBeforeReturn()

{
}
}
public class TestAspectClass

{
public void CodeInlineAtStart()

{
Console.Write("CodeInlineAtStart");
}

public void CodeInlineBeforeReturn()

{
Console.Write("CodeInlineBeforeReturn");
}
}
Code after weaving (decompiled)
public class TestClass

{
public TestClass()

{
Console.Write("CodeInlineAtStart");
Console.Write("CodeInlineBeforeReturn");
}

public TestClass(object )

{
Console.Write("CodeInlineAtStart");
Console.Write("CodeInlineBeforeReturn");
}

public void TestInlineAtStartBeforeReturn()

{
Console.Write("CodeInlineAtStart");
Console.Write("CodeInlineBeforeReturn");
}
}
7.3.7 AroundBody
The advice AroundBody can only be apply to Method that means uses specific Method code body in aspect assembly to replace Methodcode bodies in base assembly.
Configuration
<AroundBody>
<Pointcut targetXPath="//Method[@name='MethodToBeAroundedBody']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAroundedMethodBody']"></Code>
</AroundBody>
<AroundBody>
<Pointcut targetXPath="//Method[@name='set_PropertyToBeAroundedBody']" />
<Code aspectAssemblyUniqueName="TestAspectLib.dll" aspectXPath="//Method[@name='CodeAroundedMethodBody']"></Code>
</AroundBody>
Code before weaving
public class TestClass

{
public void MethodToBeAroundedBody()

{
Console.Write("MethodToBeAroundedBody");
}

public string PropertyToBeAroundedBody

{
set

{
}
}
}

public class TestAspectClass

{
public void CodeAroundedMethodBody()

{
Console.Write("CodeAroundedMethodBody");
}
}

Code after weaving (decompiled)
public class TestClass

{
public void MethodToBeAroundedBody()

{
Console.Write("CodeAroundedMethodBody");
}

public string PropertyToBeAroundedBody

{
set

{
Console.Write("CodeAroundedMethodBody");
}
}
}7.4 Additional Custom Attribute Configuration Way
TestAspectClass.cs
using System;
using AspectWeaver.Apects;

namespace TestAspectLib


{
public class TestAspectClass

{
[Introduce("//Type[@name='TestClass']")]
private string fieldToBeIntroducedByCustomAttribute = "fieldToBeIntroducedByCustomAttribute";

[Introduce("//Type[@name='TestClass']")]
public void MethodToBeIntrodcuedByCustomAttribute()

{
}
}

[Introduce()]
public class ClassToBeIntroducedByCustomAttribute

{
}
}
This sample is part of sample with both meta way and custom attribute way configuration within AspectWeaver source code. Be careful, don't define the same advice both in meta xml and custom attribute, or that may cause error. And only the advices with a code element can be configurated with the new custom attribute way. Advices can be configurated by the new way are: Introduce, BeforeConstructor/AfterConstructor, BeforeMethodCall/AfterMethodCall, InlineAtStart, InlineBeforeReturn and AroundBody.
7.5 How to Get Runtime Method Context Info and Method Arguments
TestClass.cs
using System;

namespace TestLib


{
public class TestClass

{
public void MethodToBeTestedGetContextInfoAndGetArgumments(object p1, string p2, int p3)

{
}

public void MethodToTestGetContextInfoAndGetArgumments()

{
MethodToBeTestedGetContextInfoAndGetArgumments("p1", "p2", 3);
}
}
}
TestAspectClass.cs
using System;
using AspectWeaver.Aspects;

namespace TestAspectLib


{
public class TestAspectClass : Aspect

{
[InlineAtStart("//Method[@name='MethodToBeTestedGetContextInfoAndGetArgumments']")]
public void CodeToTestGetContextInfoAndGetArgumments()

{
string contextInfo = GetContextInfo();
Console.Write(contextInfo + "\n\n");
object[] args = GetArguments();
Console.Write(string.Format("Arguments: {0}, {1}, {2}\n\n", args));
}
}
}
After Weaving Code (decompiled)
public class TestClass

{
public void MethodToBeTestedGetContextInfoAndGetArgumments(object p1, string p2, int p3)

{
String string1 = "MethodToBeTestedGetContextInfoAndGetArgumments(System.Object,System.String,System.Int32)";
Console.Write(String.Concat(contextInfo, "\n\n"));

object[] locals = new object[]
{p1, p2, p3};
Console.Write(String.Format("Arguments: {0}, {1}, {2}\n\n", locals));
}

public void MethodToTestGetContextInfoAndGetArgumments()

{
MethodToBeTestedGetContextInfoAndGetArgumments("p1", "p2", 3);
}
}
7.6 Download Source Code And Sample (Updated: 2005/10/05)
AspectWeaver0.6.zip
AspectWeaverSample1.0.zip [More About AspectWeaverSample1.0]
[More about Teddy's Aspect Weaver]
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构