在 C# 或 .NET 中是否有一种方法可以在调用方法时触发事件的方法上创建属性?理想情况下,我将能够在调用该方法之前和之后运行自定义操作。
我的意思是这样的:
[TriggersMyCustomAction()]
public void DoSomeStuff()
{
}
我完全不知道该怎么做,或者如果可能的话,但是System.Diagnostic.ConditionalAttribute可能会在后台做类似的事情。不过我不确定。
编辑:我忘了提及,由于我的具体情况,性能并不是真正的问题。
最佳答案
中使用了这个概念MVC网络应用程序。
.NET Framework 4.x 提供了几个触发操作的属性,例如:ExceptionFilterAttribute
(处理异常)、AuthorizeAttribute
(处理授权) ).两者都在 System.Web.Http.Filters
中定义。
例如,您可以按如下方式定义自己的授权属性:
public class myAuthorizationAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
// do any stuff here
// it will be invoked when the decorated method is called
if (CheckAuthorization(actionContext))
return true; // authorized
else
return false; // not authorized
}
}
然后,在您的 controller 类中,您按如下方式装饰应该使用您的授权的方法:
[myAuthorization]
public HttpResponseMessage Post(string id)
{
// ... your code goes here
response = new HttpResponseMessage(HttpStatusCode.OK); // return OK status
return response;
}
每当调用 Post
方法时,它都会在 myAuthorization
属性 之前调用 IsAuthorized
方法Post
方法中的代码被执行。
如果您在 IsAuthorized
方法中返回 false
,则表明授权未被授予,Post
方法的执行将中止。
为了理解它是如何工作的,让我们看一个不同的例子:ExceptionFilter
,它允许通过使用属性来过滤异常,用法类似于上面所示的AuthorizeAttribute
(您可以找到有关其用法的更详细说明 here )。
要使用它,请从 ExceptionFilterAttribute
派生 DivideByZeroExceptionFilter
类,如图 here , 并覆盖方法 OnException
:
public class DivideByZeroExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is DivideByZeroException)
{
actionExecutedContext.Response = new HttpResponseMessage() {
Content = new StringContent("A DIV error occured within the application.",
System.Text.Encoding.UTF8, "text/plain"),
StatusCode = System.Net.HttpStatusCode.InternalServerError
};
}
}
}
然后使用下面的演示代码来触发它:
[DivideByZeroExceptionFilter]
public void Delete(int id)
{
// Just for demonstration purpose, it
// causes the DivideByZeroExceptionFilter attribute to be triggered:
throw new DivideByZeroException();
// (normally, you would have some code here that might throw
// this exception if something goes wrong, and you want to make
// sure it aborts properly in this case)
}
现在我们知道它是如何使用的,我们主要对实现感兴趣。以下代码来自 .NET Framework。它在内部使用接口(interface) IExceptionFilter
作为契约:
namespace System.Web.Http.Filters
{
public interface IExceptionFilter : IFilter
{
// Executes an asynchronous exception filter.
// Returns: An asynchronous exception filter.
Task ExecuteExceptionFilterAsync(
HttpActionExecutedContext actionExecutedContext,
CancellationToken cancellationToken);
}
}
ExceptionFilterAttribute
本身定义如下:
namespace System.Web.Http.Filters
{
// Represents the attributes for the exception filter.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,
Inherited = true, AllowMultiple = true)]
public abstract class ExceptionFilterAttribute : FilterAttribute,
IExceptionFilter, IFilter
{
// Raises the exception event.
// actionExecutedContext: The context for the action.
public virtual void OnException(
HttpActionExecutedContext actionExecutedContext)
{
}
// Asynchronously executes the exception filter.
// Returns: The result of the execution.
Task IExceptionFilter.ExecuteExceptionFilterAsync(
HttpActionExecutedContext actionExecutedContext,
CancellationToken cancellationToken)
{
if (actionExecutedContext == null)
{
throw Error.ArgumentNull("actionExecutedContext");
}
this.OnException(actionExecutedContext);
return TaskHelpers.Completed();
}
}
}
在 ExecuteExceptionFilterAsync
中,方法 OnException
被调用。因为您已经如前所示覆盖了它,所以现在可以通过您自己的代码处理该错误。
OwenP 的回答 PostSharp 中也提到了商业产品。 ,这使您可以轻松地做到这一点。 Here是如何使用 PostSharp 做到这一点的示例。请注意,有一个 Express 版本可用,您甚至可以在商业项目中免费使用它。
PostSharp 示例(有关完整说明,请参阅上面的链接):
public class CustomerService
{
[RetryOnException(MaxRetries = 5)]
public void Save(Customer customer)
{
// Database or web-service call.
}
}
这里的属性指定如果发生异常,Save
方法最多被调用 5 次。以下代码定义了此自定义属性:
[PSerializable]
public class RetryOnExceptionAttribute : MethodInterceptionAspect
{
public RetryOnExceptionAttribute()
{
this.MaxRetries = 3;
}
public int MaxRetries { get; set; }
public override void OnInvoke(MethodInterceptionArgs args)
{
int retriesCounter = 0;
while (true)
{
try
{
args.Proceed();
return;
}
catch (Exception e)
{
retriesCounter++;
if (retriesCounter > this.MaxRetries) throw;
Console.WriteLine(
"Exception during attempt {0} of calling method {1}.{2}: {3}",
retriesCounter, args.Method.DeclaringType, args.Method.Name, e.Message);
}
}
}
}
关于C#:如何在调用时触发事件的方法上创建属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/226420/