ExceptionFilterAttribute

文章介绍了Flatwhite.Core命名空间中的两个主要组件:ExceptionFilterAttribute基类,用于处理方法级别的异常过滤;以及ExtensionMethods静态类,提供了扩展方法,包括从字典尝试获取对象和不明确泛型类型的任务结果获取。这些组件主要用于.NET框架的异步处理和错误管理。
摘要由CSDN通过智能技术生成

目录

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

{

/// <summary>

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

/// </summary>

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

    public abstract class ExceptionFilterAttribute : Attribute

    {

        /// <summary>

        /// Occurs when there is an exception

        /// </summary>

        /// <param name="exceptionContext"></param>

        [DebuggerStepThrough]

        public virtual void OnException(MethodExceptionContext exceptionContext)

        {

        }

        /// <summary>

        /// Occurs when there is an exception

        /// </summary>

        /// <param name="exceptionContext"></param>

        /// <returns></returns>

        [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

{

    /// <summary>

    1. Provide some extension methods

    /// </summary>

    public static class ExtensionMethods

    {

        /// <summary>

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

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

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="dictionary"></param>

        /// <param name="key">The key to get the object</param>

        /// <param name="default">Default value if dictionary doesn't contain key</param>

        /// <returns></returns>

        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;

        }

        /// <summary>

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

        /// </summary>

        /// <param name="unknownGenericArgumentTypeTaskWithResult"></param>

        /// <returns></returns>

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

        {

            await unknownGenericArgumentTypeTaskWithResult;

            dynamic taskWithResult = unknownGenericArgumentTypeTaskWithResult;

            return taskWithResult.Result;

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_74456535

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值