Can an Attribute be used to prevent a method from being called?
NickName:Ryan Ask DateTime:2018-01-18T08:16:16

Can an Attribute be used to prevent a method from being called?

I'm trying to write a custom attribute that I can apply to methods that will prevent the method from being called when Transaction.Current is null. I currently have a basic attribute class that throws an exception if that item is null. Here it is:

[AttributeUsage(AttributeTargets.Method)]
public class RequiresTransactionAttribute : System.Attribute
{
    public RequiresTransactionAttribute()
    {
        if (Transaction.Current == null)
        {
            throw new Exception("requires transaction");
        }
    }
}

The problem is that this does not cause method calls to fail in my tests.

I've read that these attributes are converted to meta data and so this constructor is never executed because it's optimized out unless I specifically use reflection to cause the attribute to be executed.

Is it possible to prevent a method from being called based on such a requirement as mine using attribute approach, or perhaps some other better method?

Copyright Notice:Content Author:「Ryan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48312029/can-an-attribute-be-used-to-prevent-a-method-from-being-called

More about “Can an Attribute be used to prevent a method from being called?” related questions

Can an Attribute be used to prevent a method from being called?

I'm trying to write a custom attribute that I can apply to methods that will prevent the method from being called when Transaction.Current is null. I currently have a basic attribute class that thr...

Show Detail

Prevent Method from being Trimmed

I'm trying to prevent IL Linker from trimming a method in my .NET WebAssembly. Because the method Callback() is called externally, the static analysis believes it is not used and removes it. Ther...

Show Detail

Prevent method from being called twice in a class that can be called with "with"

I'm writing a class that can be used with a with statement like so: with Context() as context: if context: ... The class has an enter function that should only be called once and retu...

Show Detail

Java: prevent an object reference from being used outside a method

I've defined an interface RecordVisitor, declared as follows: public interface RecordVisitor<K, V, Result> { public Result visit(Record<K, V> rec); } The idea is that implementati...

Show Detail

Custom Authorize Attribute being called even if attribute has not being used

I'm having the following issue with ASP.Net MVC 5. The OnAuthorization method is being called even though I haven't added the attribute anywhere in my code. [AttributeUsage(AttributeTargets.Class |

Show Detail

How to prevent a method from being invoked?

How can I prevent a method from being invoked? It's in a class imported from a jar and hence, I cannot edit the class. Reason : I want to prevent it since that method calls another REST API and ge...

Show Detail

Is there a way to prevent a base class method from being called on an instance of a derived class?

I have a base class with a bunch of functionality and a derived class that extends that class but there are a few methods in the base class that don't make sense on the derived class. Is it possib...

Show Detail

Prevent a method from being called if it is incorrect using __getattr__

I am trying to prevent an instance from throwing an exception if a method that does not exist for the instance is called. I have tried the following: class myClass(object): def __init__(self):...

Show Detail

Prevent particular Action method from being overriden by base class

I have a controller class inheriting a base class that handles/overrides a virtual method (say JsonResult), and in this controller I need to prevent a specific JsonResult method (and only this one...

Show Detail

Method not intended for code attribute?

I have a couple of shared methods in my app which are intended to be used with ObjectDataSource's (and other reflection based objects). They handle a lot of automatic parameter checking, filtering...

Show Detail