ExceptionFilterAttribute

目录

1 ExceptionFilterAttribute

1.1 Represents the base class for all method-filter attributes.

2 ExtensionMethods

2.1 Provide some extension methods

2.1.1 Try to get a result of a Task when we don't know it's generic argument type

  1. ExceptionFilterAttribute 

using System;

using System.Diagnostics;

using System.Threading.Tasks;

namespace Flatwhite.Core

{

/// 

    1. Represents the base class for all method-filter attributes.

/// 

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]

    public abstract class ExceptionFilterAttribute : Attribute

    {

        /// 

        /// Occurs when there is an exception

        /// 

        /// exceptionContext">

        [DebuggerStepThrough]

        public virtual void OnException(MethodExceptionContext exceptionContext)

        {

        }

        /// 

        /// Occurs when there is an exception

        /// 

        /// exceptionContext">

        /// 

        [DebuggerStepThrough]

        public virtual Task OnExceptionAsync(MethodExceptionContext exceptionContext)

        {

OnException(exceptionContext);

            return Task.CompletedTask;

        }

    }

}

  1. ExtensionMethods

using System.Collections.Generic;

using System.Threading.Tasks;

namespace Flatwhite.Core

{

    /// 

    1. Provide some extension methods

    /// 

    public static class ExtensionMethods

    {

        /// 

        /// Try to get the object from dictionary by key

        /// If not found, return the provided default value

        /// 

        /// T">

        /// dictionary">

        /// key">The key to get the object

        /// default">Default value if dictionary doesn't contain key

        /// 

        public static T TryGetByKey<T>(this IDictionary<string, object> dictionary, string key, T @default = default(T))

        {

            if (dictionary == null || key == null || !dictionary.ContainsKey(key))

            {

                return default(T);

            }

            var obj = dictionary[key];

            if (!(obj is T))

            {

                return default(T);

            }

            return (T)obj;

        }

        /// 

      1.       Try to get a result of a Task when we don't know it's generic argument type

        /// 

        /// unknownGenericArgumentTypeTaskWithResult">

        /// 

        internal static async Task<object> TryGetTaskResult(this Task unknownGenericArgumentTypeTaskWithResult)

        {

            await unknownGenericArgumentTypeTaskWithResult;

            dynamic taskWithResult = unknownGenericArgumentTypeTaskWithResult;

            return taskWithResult.Result;

        }

    }

}

你可能感兴趣的:(Flatwhite,java,开发语言)