Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Attributes to Call Methods

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.

like image 832
keyboardP Avatar asked Nov 03 '12 16:11

keyboardP


People also ask

What are method attributes?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ).

What is use of attributes in C#?

In C#, attributes are classes that inherit from the Attribute base class. Any class that inherits from Attribute can be used as a sort of "tag" on other pieces of code. For instance, there is an attribute called ObsoleteAttribute . This is used to signal that code is obsolete and shouldn't be used anymore.

What is AC attribute?

In computer security, an attribute certificate, or authorization certificate (AC) is a digital document containing attributes associated to the holder by the issuer.

What is obsolete method in C#?

An obsolete attribute, in C#, is a declarative tag used while declaring a type or a member of a type to indicate that it should no longer be used.


2 Answers

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

like image 186
maximpa Avatar answered Sep 19 '22 14:09

maximpa


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.

like image 34
gnz Avatar answered Sep 21 '22 14:09

gnz