. NET in AOP convenient god Sheepaspect

Source: Internet
Author: User

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)
  • Interface
  • ! Interface
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.