Sheepaspect Introduction and Code Listing:
Sheepaspect is an AOP framework for the. NET platform, deeply aspectj. It statically weaves the target component as a compiled task (the AOP code is implanted at compile time).
When there are many characteristics, you can set the sequential order according to the parameter value.
The following begins the Code implementation tour:
First, the new console program: scheme name: sheepaecttest
Second, NuGet search sheepaspect for installation
Three, after the installation of the appearance
The entry type of the member (Selectmethdos):
"SAQL":
| Criteria |
Argument |
Examples |
| Name |
String |
- Name: ' *customer '
- Name: (' *service ' | ' *repository ')
|
| Namespace |
String |
- ! Namespace: ' system.* '
|
| Implementstype |
Type Pointcut |
- Implementstype: ' System.Collections.IEnumerable '
- Implementstype: (Name: ' *repository ' | Namespace: ' *. Datacontexts ')
|
| Assignabletotype |
Type Pointcut |
- Assignabletotype: (' system.collections.* ' & Interface)
- Assignabletotype:namespace: ' system.collections.* '
|
| Hasmethod |
Method Pointcut |
- Hasmethod:name: ' get* '
- Hasmethod: (Public & Args (' System.Int32 ')
|
| Hasproperty |
Property Pointcut |
- Hasproperty:name: ' Length '
- HasProperty:Type:Implements: ' *.*service '
|
| Hasfield |
Field Pointcut |
- Hasfield:name: (' _createddate ' | ' _entrydate ')
- Hasfield: ((Public & Static) | Protected)
|
| Thisaspect |
(none) |
- Thisaspect
- Implements:thisaspect
- Namespace: ' sheep.* ' &! Thisaspect
|
| Hascustomattributetype |
Type Pointcut |
- Hascustomattributetype:implementstype: ' Bindableattribute '
|
| inheritstype* |
Type Pointcut |
- Inheritstype:namespace: ' System.Collections '
|
| interface* |
(none) |
|
| abstract* |
(none) |
- Abstract & Name: ' *strategy '
|
| valuetype* |
(none) |
- ValueType & hasmethod:name: ' Equals '
|
| class* |
(none) |
- Class & Implements: ' Sheep.irepository '
|
Attributes are implanted in the following columns:
First, new features
Public class Logattribute:attribute { publicstringgetset;} Public Logattribute (string name) { = name; } }
Second, a new test class TestClass.cs
public class TestClass { [Log (first method obtained]] public string Get () { return "test1"; } public string Get2 () { return ' test2 '; } }
Third, change Sampleaspect to: hascustomattributetype: ' SheepAectTest.Attr.LogAttribute ' > namespace + class name
Four: Write the test code:
Output Result:
What if we change the results in AOP?
Output Result:
To get the properties of an attribute:
[Aspect] public class Sampleaspect { [Selectmethods ("Hascustomattributetype: ' SheepAectTest.Attr.LogAttribute '")] Public void Publicmethods () {} [Around ("publicmethods", priority = +)] public Object Logaroundmethod (methodjointpoint jp) { try { var log = (Logattribute) JP. Method.getcustomattributes (typeof (Logattribute), false) [0]; This allows you to get the property name: Log. Name; Jp. Args --Contains the pass parameter var result = JP. Execute (); if (JP. Method.returntype = = typeof (void)) result = "{void}"; result = "AOP change result"; return result; } catch (Exception e) { throw; } } }
Multiple attribute Injection order: The Priority attribute value controls the lower precedence value;
Crazy Fruit Source: http://incsharp.cnblogs.com/Statement: Original Blog Please keep the original link in the reprint or at the beginning of the article with my blog address, such as found errors, welcome criticism. If you have special needs, please contact with me!
433685124QQ Group
. NET in AOP convenient god Sheepaspect