Click here to Skip to main content
11,921,889 members (68,414 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked

Introducing the KingAOP Framework: Part 1

, 24 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
KingAOP - It's an AOP framework which is essentially a free alternative of PostSharp.


KingAOP - It's an AOP framework which is essentially a free alternative of PostSharp. If you are familiar with PostSharp, then you can notice that KingAOP has the similar interfaces. The main reason of creation of KingAOP was that currently we have several frameworks which provide a nice opportunities for AOP but no one has so simple and explicit interaction with developer as PostSharp. I like API of PostSharp because it's predictable and anyone can understand it from first glance. The second reason was, there are many questions on stackoverflow about free alternatives of PostSharp. It was pretty valuable argument to start development. The third reason was requirement to obtain the easy and transparent access to weaving code. Also I wanted to get rid of dependency in generating code in post compile phase via some third party IL generator. Instead of it I use the native C# compiler without shifting duties on some non official IL generator. On that moment I was really digging into "dynamic" C# and hence I realized that it's possible to get all AOP things which will based on "dynamic" + "DLR Trees"(for metaprogramming).


This article assumes that you're familiar with the concept of AOP and PostSharp framework.

Using the code

In this article I going to show you the basic and simple concept of interception methods. Let's start our diving in AOP world via well known MethodInterceptionAspect aspect. This aspect allows you to intercept invocation of method call and do anything what you want. For example you can put the additional validation, caching, logging, wait/retry, automatic thread delegation, lazy loading, invoke additional methods or just not invoke original method and so on. Actually this aspect replaces the method body with a call to the aspect, then moves the original body elsewhere.

Our test example will be: to invoke method and print passed arguments with values. I can agree it's not so useful requirement but it show you the basic concept. In next parts we will look on more useful examples.

So to try KingAOP power let's create simple PrintArgsAspect class inherited from MethodInterceptionAspect with overridden OnInvoke method. Basically it will be our test aspect for experiments. PrintArgsAspect will be responsible to print arguments values.

class PrintArgsAspect : MethodInterceptionAspect
    public override void OnInvoke(MethodInterceptionArgs args)
    { }

The OnInvoke method will be invoked instead of real method and here is the place to do useful things. As you can see we acquire all arguments via MethodInterceptionArgs, this entry point allow us to make decision what we will do next: change arguments, throw exception, invoke original method, skip invocation of original method and so on. As long as we wanted just to take argument's value and print it, let's do that.

class PrintArgsAspect : MethodInterceptionAspect
    public override void OnInvoke(MethodInterceptionArgs args)
        // print arguments values
        string logData = PrintArgs("OnInvoke", args);
        // invoke original method

    string PrintArgs(string methodStage, MethodInterceptionArgs args)
        var str = new StringBuilder();
        str.AppendLine(string.Format(methodStage + " {0} ", args.Method));

        foreach (var argument in args.Arguments)
            var argType = argument.GetType();

            str.Append(argType.Name + ": ");

            if (argType == typeof(string) || argType.IsPrimitive)
                foreach (var property in argType.GetProperties())
                    str.AppendFormat("{0} = {1}; ",
                        property.Name, property.GetValue(argument, null));
        return str.ToString();

Also please notice that to invoke original method we call Proceed method. Let's assume that we have TestRepository class with Save method where we want to apply our aspect.

class TestRepository
    public void Save(TestEntity entity)
    { }

Also we have TestEntity class which is our argument of Save method of TestRepository class.

class TestEntity
    public int Number { get; set; }
    public string Name { get; set; }

In order to apply some aspect of KingAOP we should inherit TestRepository from IDynamicMetaObjectProvider interface. It's necessary for DLR to invoke our aspect via dynamic. You can think about it as indicator for DLR to do dynamic magic. The IDynamicMetaObjectProvider require to implement GetMediaObject method. This method should always returns KingAOP's AspectWeaver object which will generate code of aspect.

class TestRepository : IDynamicMetaObjectProvider
    [PrintArgsAspect] // apply aspect
    public void Save(TestEntity entity)
    { }

    // need for AOP weaving
    public DynamicMetaObject GetMetaObject(Expression parameter)
        return new AspectWeaver(parameter, this);

The above implementation has method Save with applied PrintArgsAspect aspect and now if you invoke Save method like this testRepository.Save(entity); the PrintArgsAspect will intercept Save (original) method invocation and print arguments. It's time to show the demo, let's look on it.

class Program
    static void Main(string[] args)
        var entity = new TestEntity { Name = "Jon", Number = 99 };
        dynamic repository = new TestRepository();
        repository.Save(entity); // this method call will be intercepted by our aspect

On console output we will have:

As you have noticed we printed argument values which passed to method Save and we used dynamic keyword. Without dynamic it doesn't work. Basically dynamic just tells C# compiler to generate necessary code for DLR to invoke our aspect.  

Points of Interest  

If you are curious about speed I would say that dynamic code blazingly fast. It's not a joke! After first invocation of method the DLR compiles all code to native IL and gets very optimized delegate which will be invoked in further calls. In other words you should think about dynamic as a normal C# code.


The entire KingAOP framework is licensed under the terms of the MIT license, meaning that you are free to use the library DLL in your commercial applications.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Antya Dev
Technical Lead
Ukraine Ukraine
I work primarily with the .NET technology stack, and specialize in accelerated code production via code generation (static or dynamic), aspect-oriented programming, domain-specific languages.

You may also be interested in...

Comments and Discussions

QuestionNot working in an inheritance case Pin
danaea30-Mar-15 8:41
memberdanaea30-Mar-15 8:41 
QuestionWhat is included exactly? Pin
wolfmanncsu200029-Jul-13 5:42
memberwolfmanncsu200029-Jul-13 5:42 
AnswerRe: What is included exactly? Pin
Antya Dev29-Jul-13 13:35
memberAntya Dev29-Jul-13 13:35 
GeneralGood work! Pin
Champion·Chen25-Jul-13 17:05
memberChampion·Chen25-Jul-13 17:05 
GeneralRe: Good work! Pin
Antya Dev25-Jul-13 22:57
memberAntya Dev25-Jul-13 22:57 
GeneralMessage Removed Pin
Arash M. Dehghani25-Jul-13 11:49
memberArash M. Dehghani25-Jul-13 11:49 
GeneralRe: My vote of 5 Pin
Antya Dev25-Jul-13 22:58
memberAntya Dev25-Jul-13 22:58 
GeneralMy vote of 5 Pin
aliHmer25-Jul-13 5:32
memberaliHmer25-Jul-13 5:32 
GeneralRe: My vote of 5 Pin
Antya Dev25-Jul-13 8:29
memberAntya Dev25-Jul-13 8:29 
GeneralRe: My vote of 5 Pin
Darek Danielewski25-Jul-13 8:50
memberDarek Danielewski25-Jul-13 8:50 
GeneralMy vote of 4 Pin
Darek Danielewski25-Jul-13 2:09
memberDarek Danielewski25-Jul-13 2:09 
GeneralRe: My vote of 4 Pin
Antya Dev25-Jul-13 8:29
memberAntya Dev25-Jul-13 8:29 
GeneralMy vote of 5 Pin
Dr Herbie25-Jul-13 1:29
memberDr Herbie25-Jul-13 1:29 
GeneralRe: My vote of 5 Pin
Antya Dev25-Jul-13 8:29
memberAntya Dev25-Jul-13 8:29 
QuestionPretty cool Pin
Sacha Barber25-Jul-13 0:09
mvpSacha Barber25-Jul-13 0:09 
5 from me
Sacha Barber
  • Microsoft Visual C# MVP 2008-2012
  • Codeproject MVP 2008-2012
Open Source Projects

Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog :

AnswerRe: Pretty cool Pin
Antya Dev25-Jul-13 0:30
memberAntya Dev25-Jul-13 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 24 Jul 2013
Article Copyright 2013 by Antya Dev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid