... | ... | @@ -7,213 +7,7 @@ Aspects is a AOP tool based on Fody, that allow you to encapsulate your code wit |
|
|
```sh
|
|
|
PM> Install-Package Aspects -Pre -Source https://www.myget.org/F/kbeaugrand-public/api/v3/index.json
|
|
|
```
|
|
|
### Write your aspect attribute
|
|
|
```cs
|
|
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
|
|
|
public class MyAspectAttribute : Attribute, IAspect
|
|
|
{
|
|
|
public string StringParameter { get; set; }
|
|
|
|
|
|
public int NumericParameter { get; set; }
|
|
|
|
|
|
public int Order => 0;
|
|
|
|
|
|
public void Init(object instance, MethodBase method, string[] argNames, object[] args)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnEntry()
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnException(Exception exception)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnExit(object result)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Add your attribute to your methods
|
|
|
```cs
|
|
|
public int TestValue
|
|
|
{
|
|
|
get; [MyAspect]set;
|
|
|
}
|
|
|
|
|
|
[MyAspect(StringParameter = "AspectParameterValue")]
|
|
|
public void MyMethod()
|
|
|
{
|
|
|
...
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Modify your FodyWeavers.xml file
|
|
|
```xml
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
<Weavers xmlns="http://tempuri.org/Aspects.xsd">
|
|
|
<Aspects>
|
|
|
<AspectReferences>
|
|
|
<AspectReferences>
|
|
|
<Assembly FullName="AssemblyToProcess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
|
|
<Aspect Name="MyAspect" Reference="AssemblyToProcess.MyAspectAttribute" />
|
|
|
</Assembly>
|
|
|
</AspectReferences>
|
|
|
</Aspects>
|
|
|
</Weavers>
|
|
|
```
|
|
|
|
|
|
## What gets compiled ?
|
|
|
|
|
|
```cs
|
|
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
|
|
|
public class MyAspectAttribute : Attribute
|
|
|
{
|
|
|
public string StringParameter { get; set; }
|
|
|
|
|
|
public int NumericParameter { get; set; }
|
|
|
|
|
|
public int Order => 0;
|
|
|
|
|
|
public void Init(object instance, MethodBase method, string[] argNames, object[] args)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnEntry()
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnException(Exception exception)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
|
|
|
public void OnExit(object result)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public int TestValue
|
|
|
{
|
|
|
[CompilerGenerated]
|
|
|
get
|
|
|
{
|
|
|
return this.<TestValue>k__BackingField;
|
|
|
}
|
|
|
[MyAspect, CompilerGenerated]
|
|
|
set
|
|
|
{
|
|
|
string[] array = new string[1];
|
|
|
object[] array2 = new object[1];
|
|
|
array[0] = "value";
|
|
|
array2[0] = value;
|
|
|
MethodBase methodFromHandle = MethodBase.GetMethodFromHandle(methodof(Class1.set_TestValue(int)).MethodHandle, typeof(Class1).TypeHandle);
|
|
|
AspectAttribute aspectAttribute = (AspectAttribute)Activator.CreateInstance(typeof(AspectAttribute));
|
|
|
aspectAttribute.Init(this, methodFromHandle, array, array2);
|
|
|
try
|
|
|
{
|
|
|
aspectAttribute.OnEntry();
|
|
|
this.<TestValue>k__BackingField = value;
|
|
|
}
|
|
|
catch (Exception exception)
|
|
|
{
|
|
|
aspectAttribute.OnException(exception);
|
|
|
throw;
|
|
|
}
|
|
|
aspectAttribute.OnExit(null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[MyAspect(StringParameter = "AspectParameterValue")]
|
|
|
public void MyMethod()
|
|
|
{
|
|
|
MethodBase methodFromHandle = MethodBase.GetMethodFromHandle(methodof(MyClass.MyMethod()).MethodHandle, typeof(MyClass).TypeHandle);
|
|
|
LibraryToWeave.Aspects.FirstAspect myAspect = (MyAspect)Activator.CreateInstance(typeof(MyAspect));
|
|
|
myAspect.StringParameter = "AspectParameterValue";
|
|
|
myAspect.Init(this, methodFromHandle, null, null);
|
|
|
try
|
|
|
{
|
|
|
myAspect.OnEntry();
|
|
|
|
|
|
...
|
|
|
}
|
|
|
catch (Exception exception)
|
|
|
{
|
|
|
myAspect.OnException(exception);
|
|
|
throw;
|
|
|
}
|
|
|
|
|
|
myAspect.OnExit(null);
|
|
|
return;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
## How to inject aspects ?
|
|
|
Modify your FordyWeavers.xml file like this:
|
|
|
|
|
|
```xml
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
<Weavers xmlns="http://tempuri.org/Aspects.xsd">
|
|
|
<Aspects>
|
|
|
<AspectReferences>
|
|
|
<Assembly FullName="AssemblyToProcess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
|
|
<Aspect Name="AspectWithProperties" Reference="AssemblyToProcess.AspectWithPropertiesAttribute">
|
|
|
<Parameters>
|
|
|
<Parameter Name="StringParameter" Value="Global parameter" />
|
|
|
</Parameters>
|
|
|
</Aspect>
|
|
|
</Assembly>
|
|
|
</AspectReferences>
|
|
|
<MethodReferences>
|
|
|
<Method Reference="System.Void AssemblyToProcess.InjectedClass::TestWithNoResult()">
|
|
|
<Inject Name="AspectWithProperties">
|
|
|
<Parameters>
|
|
|
<Parameter Name="StringParameter" Value="Method parameter" />
|
|
|
<Parameter Name="NumericParameter" Value="10" />
|
|
|
</Parameters>
|
|
|
</Inject>
|
|
|
</Method>
|
|
|
<Method Reference="System.Int32 AssemblyToProcess.InjectedClass::TestWithResult()">
|
|
|
<Inject Name="AspectWithProperties">
|
|
|
<Parameters>
|
|
|
<Parameter Name="NumericParameter" Value="100" />
|
|
|
</Parameters>
|
|
|
</Inject>
|
|
|
</Method>
|
|
|
</MethodReferences>
|
|
|
</Aspects>
|
|
|
</Weavers>
|
|
|
```
|
|
|
|
|
|
#### ```AspectReferences```
|
|
|
Add your aspect references here. You must specify theses:
|
|
|
|
|
|
##### ```Name```
|
|
|
A declarative name for your aspect
|
|
|
|
|
|
##### ```Reference```
|
|
|
The aspect fully qualified name.
|
|
|
|
|
|
##### ```Parameters```
|
|
|
If your aspect has some properties you could specify theire value here. (Here is the global values.)
|
|
|
|
|
|
#### ```MethodReferences```
|
|
|
Add the methods you want to inject:
|
|
|
|
|
|
##### ```Reference```
|
|
|
The method fully qualified name.
|
|
|
|
|
|
##### ```Inject Name```
|
|
|
The aspect declarative name setted above
|
|
|
|
|
|
##### ```Parameters```
|
|
|
If your aspect has some properties you could specify theire value here. (Here is the method specific values.) |
|
|
\ No newline at end of file |
|
|
## Wiki summary
|
|
|
* [Home](home)
|
|
|
* [Write an aspect](Write-and-aspect) |
|
|
\ No newline at end of file |