目录
1 MethodInvocationContext
1.1 Contains basic information about a method action
2 ServiceFilterAttribute
2.1 A filter that finds another filter in an
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace Flatwhite.Core
{
///
///
public abstract class MethodInvocationContext
{
///
/// The MethodInfo of the executing method
///
public MethodInfo MethodInfo { get; internal set; }
///
/// The invocation context of the executing method
///
public IDictionary<string, object> InvocationContext { get; internal set; }
///
/// The result of the method being filtered. Set value to stop the chain
///
public object Result {get;set;}
///
/// The Invocation data
///
[EditorBrowsable(EditorBrowsableState.Never)]
public IInvocation Invocation { get; internal set; }
}
}
using System;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
namespace Flatwhite.Core
{
///
/// https://github.com/aspnet/Mvc/blob/65af12f1f575fdaee893e0232416df29bb83d7fa/src/Microsoft.AspNetCore.Mvc.Core/ServiceFilterAttribute.cs
///
///
///
/// Similar to the
///
///
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("ServiceFilter: Type={ServiceType} Order={Order}")]
public class ServiceFilterAttribute : Attribute, IFilterFactory
{
///
/// Instantiates a new
///
/// type">The
public ServiceFilterAttribute(Type type)
{
ServiceType = type ?? throw new ArgumentNullException(nameof(type));
}
///
/// Gets or sets the order in which the action filters are executed.
///
public int Order { get; set; }
///
/// Gets the
///
public Type ServiceType { get; }
///
public MethodFilterAttribute CreateInstance(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
var filter = (MethodFilterAttribute)serviceProvider.GetRequiredService(ServiceType);
if (filter is IFilterFactory filterFactory)
{
// Unwrap filter factories
filter = filterFactory.CreateInstance(serviceProvider);
}
filter.Order = Order;
return filter;
}
}
}