目录
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
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Flatwhite.Core
{
///
///
[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;
}
}
}
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Flatwhite.Core
{
///
///
public static class ExtensionMethods
{
///
/// Try to get the object from dictionary by key
/// If not found, return the provided default value
///
///
/// 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;
}
///
///
/// unknownGenericArgumentTypeTaskWithResult">
///
internal static async Task<object> TryGetTaskResult(this Task unknownGenericArgumentTypeTaskWithResult)
{
await unknownGenericArgumentTypeTaskWithResult;
dynamic taskWithResult = unknownGenericArgumentTypeTaskWithResult;
return taskWithResult.Result;
}
}
}