Coder Social home page Coder Social logo

tony.interceptor_bf's Introduction

Welcome to Tony.Interceptor

This is a project written by C# that can intercept instance method you want

You can do something before and do something after when you invoke the method

why use Tony.Interceptor

You can image you have write thousands of methods.one day ,your boss requires you to add the log for each method,you are driven mad.Would you want to write the log code in each method?

or you use the third part AOP Framework?That is very heavy

No,this is the reason you use Tony.Intercetor!!!

usage

1.define a class that implement the interface IInterceptor:

so you can handle the BeforeInvoke and AfterInvoke

class LogInterceptor : IInterceptor
    {
        public void AfterInvoke(object result, MethodBase method)
        {
            Console.WriteLine($"执行{method.Name}完毕,返回值:{result}");
        }

        public void BeforeInvoke(MethodBase method)
        {
            Console.WriteLine($"准备执行{method.Name}方法");
        }
    }

2.markup the class or method that you want to Intercept

First of all,the class must extend to Interceptable,in fact,the class Interceptable extends from ContextBoundObject,just put the class into the environment context

Then,you can use InterceptorAttribute to mark the class or a instance method in the class

If you mark the class ,it intercepts all the public instance method by default.

If you do not want to intercept a method int the marked class,you can use InterceptorIgnoreAttribute

[Interceptor(typeof(LogInterceptor))]
    public class Test:ContextBoundObject
    {
        public void TestMethod()
        {
            Console.WriteLine("执行TestMethod方法");
        }
        public int Add(int a, int b)
        {
            Console.WriteLine("执行Add方法");
            return a + b;
        }
        [InterceptorIgnore]
        public void MethodNotIntercept()
        {
            Console.WriteLine("MethodNotIntercept");
        }
    }

3.create the instance from the class and invoke the method

class Program
{
    static void Main(string[] args)
    {
        Test test = new Test();
        test.TestMethod();
        test.Add(5,6);
        test.MethodNotIntercept();
        Console.Read();
    }
}

Global Setting

this is a switch that can enable or disable the interceptor.the switch is:

public static bool IsEnableIntercept { get; set; } = true;

the default value is true.if we set to false,the interceptor we have deployed is invalid

tony.interceptor_bf's People

Contributors

lishuangquan1987 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.