Files

48 lines
1.2 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Opsoft.NetCore.O3App.Shared
{
public static class Int32Extensions
{
public static TOut ToEnum<TOut>(this int value)
{
var outputType = typeof(TOut);
if (outputType.IsEnum)
{
if (outputType.IsEnumDefined(value))
{
var obj = Enum.ToObject(outputType, value);
if (obj != null)
{
return (TOut)obj;
}
}
}
return default(TOut);
}
/// <summary>
/// 检查某值是否在指定范围内
/// </summary>
public static bool Between(this int value, int min, int max)
{
return value >= min && value <= max;
}
public static bool In(this int source, params int[] values)
{
return values.Contains(source);
}
public static bool In(this int source, params Enum[] enums)
{
return Array.Exists(enums, current => Convert.ToInt32(current) == source);
}
}
}