Reputation: 6060
I'm trying to find out if it's possible to setup a Custom Attribute to determine if a method call can process. What I'm wanting is similar to the [Authorize()]
attribute that I use in my MVC3 application.
Basically, I have a COM Object that I'm interfacing (3rd Party DMS Software) with and I want to make sure that the user hasn't closed the program before I process the contents of the method. I can wrap the calls in a try/catch
or I can wrap a method that checks the application.running
, but I like the attribute functionality and the simple markup (if it's required).
All the tutorials/documentation that I've found so far is how to markup a method/class with properties (they generally use a string in the tutorials) and then access those string values later in code.
This is just not something that I'm wanting to do if at all possible. Again, in MVC3/4 you can do...
[Authorize()]
public class ControllerClass : Controller {
public ActionResult Index(){ return View(); }
....etc....
}
In this example Index always has to be be called by someone that passes Authorize
. What I'm not sure is if this is required to instantiate the class or call the method.
So after all of that my question is simply. Can something similar to Authorize()
be implemented for a C# WinForms Library?
Upvotes: 0
Views: 399
Reputation: 236208
Yes, you can implement it via AOP framework like PostSharp. Sample:
[Serializable]
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
if (!application.running)
throw new Exception(String.Format("Method {0} is not allowed to call when application is not running.", args.Method.Name));
}
}
Upvotes: 2