Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create C# Attribute to Suppress Method Execution

I wish to create a custom attribute that suppress a Method from being executed in C# even if it is invoked. For example, In the code block below if the method has 'Skip' Attribute it should not be executed even though it is called from Main.

public class MyClass {

  public static void main()
  {
    aMethod();  
  }

  [Skip]
  public void aMethod() {
    ..
  }

}

How can I achieve this using reflection in C# ?


In the code snippet below I have managed to extract methods that carry the Skip Attribute, I just can't figure out how to stop them from executing!

MethodInfo[] methodInfos = typeof (MyClass).GetMethods();

foreach (var methodInfo in methodInfos)
{
  if (methodInfo.HasAttribute(typeof(SkipAttribute)))
  {
    // What goes here ??
  }
}

Any kinda help or suggestion in the right direction is most welcome :)

like image 264
shantanusinghal Avatar asked May 29 '13 18:05

shantanusinghal


People also ask

What is create () in C?

1. Create: Used to Create a new empty file. Syntax in C language: int create(char *filename, mode_t mode)

How do you create a file in C?

To create a file in a 'C' program following syntax is used, FILE *fp; fp = fopen ("file_name", "mode"); In the above syntax, the file is a data structure which is defined in the standard library. fopen is a standard function which is used to open a file.

What is O_rdwr?

O_RDWR. Open for reading and writing. The result is undefined if this flag is applied to a FIFO. Any combination of the following may be used: O_APPEND.

What is Fcntl H used for?

h is the header in the C POSIX library for the C programming language that contains constructs that refer to file control, e.g. opening a file, retrieving and changing the permissions of file, locking a file for edit, etc.


1 Answers

It's not clear what you're after.

First off, @Ignore is for telling the JUnit test runner to ignore the test. You didn't mention testing in your question, but we should be clear that that is what @Ignore is for. Test runners in .NET have similar attributes (e.g., in xUnit, the attribute is [Ignore]).

So, if you're using a test runner, find the corresponding attribute for that test runner. If you're not using a test runner, what exactly are you after given that @Ignore is germane only to test running?

Are you writing your own test runner? Why? There are plenty of really good free test runners available. Use them!

I want the attribute to suppress the execution even if the method is called.

Well, that's a code smell if I ever saw one.

You have a few options.

Insert code into every method that you apply [Ignore] to:

[AttributeUsage(AttributeTargets.Method)]
public class Ignore : Attribute { }

[Ignore]
public static void M() {
    var ignoreAttributes =
        MethodBase.GetCurrentMethod().GetCustomAttributes(typeof(Ignore), true);
    if (ignoreAttributes.Any()) {
        return;
    }
    // method execution proceeds
    // do something
}

Or, you can use an interception technique.

Or, you can use a post-compilation framework.

ALL of these have very serious problems. They have problems because what you're doing is a code smell.

like image 103
jason Avatar answered Sep 19 '22 22:09

jason