Using Attributes to Call Methods

2019-03-18 12:22发布

问题:

I have various individual methods which all need to perform the same functions before continuing on with their own implementation. Now I could implement these functions in each method, but I was wondering if there's a way to exploit attributes to do this? As a very simple example, all network calls have to check for a network connection.

public void GetPage(string url)
{
   if(IsNetworkConnected())
      ...
   else
      ...           
}

This would work, but I'd have to call the IsNetworkConnected method for each method that uses the network and handle it individually. Instead, I'd like to do this

[NetworkCall]
public void GetPage(string url)
{
   ...
}

If the network is unavailable, an error method is called instead and GetPage is ignored, otherwise GetPage is invoked.

This sounds very much like Aspect Orientated Programming, but I don't want to implement an entire framework for a few calls. This is more of a learning exercise than an implementation one, so I was curious as to how something like this would be best implemented.

回答1:

You can use PostSharp, it is aspect-oriented framework for .NET, it seems quite easy to use:

static void Main(string[] args)
{
    Foo();
}

[IgnoreMethod(IsIgnored=true)]
public static void Foo()
{
    Console.WriteLine("Executing Foo()...");
}

[Serializable]
public class IgnoreMethodAttribute : PostSharp.Aspects.MethodInterceptionAspect
{
    public bool IsIgnored { get; set; }

    public override void OnInvoke(PostSharp.Aspects.MethodInterceptionArgs args)
    {
        if (IsIgnored)
        {
            return;
        }

        base.OnInvoke(args);
    }
}

Method-Level Aspects feature is available in the free edition: http://www.sharpcrafters.com/purchase/compare

Run-Time Performance:

Because PostSharp is a compiler technology, most of the expensive work is done at build time, so that applications start quickly and execute fast. When generating code, PostSharp takes the assumption that calling a virtual method or getting a static field is an expensive operation. Contrary to rumor, PostSharp does not use System.Reflection at run time. http://www.sharpcrafters.com/postsharp/performance



回答2:

I don't think you can do this with attributes only, because they are not executed by the runtime if you're not actively doing something with them. A lightweight approach would be Ninject with Interceptions extension, it is a framework, but a very thin one, and one you might already be using for DI anyway.

Another option, but a bit more involved, could be based on MEF, and then you can use attributes and do something during with them during activation.



回答3:

You're right, it sounds a lot like AOP.
What you're after sounds like compile time weaving? I.e. the attribute is turned into additional code by the compiler.
You could look at how to implement this...
Generating additional code through a custom attribute
http://www.cs.columbia.edu/~eaddy/wicca/ &
http://www.sharpcrafters.com/aop.net/compiletime-weaving
all refer to tools and techniques for doing this.

Or you could use an AOP framework. IMHO, you should look at AOP frameworks.