Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:27:49+00:00 2026-06-13T20:27:49+00:00

I have various individual methods which all need to perform the same functions before

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:27:50+00:00Added an answer on June 13, 2026 at 8:27 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have various class which perform save and update operations of GUI elements, it
I have various models of which I would like to keep track and collect
We have a sports training camp which is regularly attended by various teams in
I have a Jtree using DefaultTreeModel, each individual node contains a UserObject containing various
Does anyone have any example implementation of making individual object props readOnly/non-configurable? I mean
I have an array of objects which contains various kinds of data in it,
I have a developer counterpart who uses Eclipse, which inserts annotations in various positions
I have various time-series I'd like to correlate and present as either a csv-file
I have various settings in my ccnet.config file and wish to pass them to
I have various pieces of generated HTML/JS that I load into Activity with WebView#loadDataWithBaseURL.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.