.NET 实用扩展方法

.NET 实用扩展方法(持续更新...)

1. 字符串转换为可空数值类型(int, long, float...类似)

///


/// 将字符串转换成32位整数,转换失败返回null
///

/// 转换的字符串
/// 转换之后的整数,或null
public static int? TryParseToInt32(this string str)
{
if (string.IsNullOrWhiteSpace(str))
return null;
var result = 0;
if (int.TryParse(str, out result))
return result;
else
return null;
}

2. 去除子字符串

///


/// 去除子字符串
///

/// 原字符串
/// 要去除的字符串
/// 去除子字符串之后的结果
public static string DeSubstring(this string str, string substring)
{
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(substring) || !str.Contains(substring))
{
return str;
}

return Regex.Replace(str, Regex.Escape(substring), string.Empty);
}

///


/// 去除子字符串
///

/// 原字符串
/// 要去除的子字符串
/// 去除子字符串之后的结果
public static string DeSubstring(this string str, params string[] substrings)
{
if (string.IsNullOrEmpty(str))
return str;
if (substrings == null)
return str;
var newStr = str;
foreach (var item in substrings)
{
newStr = newStr.DeSubstring(item);
}
return newStr;
}

3. 获取子序列

///


/// 获取子序列
///

/// 序列中元素类型
/// 源数据
/// 开始索引(返回时包括)
/// 结束索引(返回时包括)
/// 子序列
public static IEnumerable SubEnumerable(this IEnumerable source, int startIndex, int endIndex)
{
if (source == null)
yield return default(T);
var length = source.Count();
if (startIndex < 0 || endIndex < startIndex || startIndex >= length || endIndex >= length)
throw new ArgumentOutOfRangeException();

var index = -1;
foreach (var item in source)
{
index++;
if (index < startIndex)
continue;
if (index > endIndex)
yield break;
yield return item;
}
}

4. 通过指定键对序列去重, 不必实现IEqualityComparer接口

///


/// 通过对指定的值进行比较返回序列中的非重复元素。
///

/// 序列中元素类型
/// 指定的比较属性类型
/// 源数据
/// 应用于每个元素的转换函数
/// 一个包含源序列中的按指定属性非重复元素
public static IEnumerable Distinct<T, TResult>(this IEnumerable source, Func<T, TResult> selector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
var set = new HashSet();
foreach (var item in source)
{
var result = selector(item);
if (set.Add(result))
{
yield return item;
}
}
}

5. 获取序列中重复的元素序列, 原理和去重类似

///


/// 通过对指定的值进行比较返回序列中重复的元素
///

/// 序列中的数据类型
/// 指定的比较属性类型
/// 源数据
/// 应用于每个元素的转换函数
/// 一个包含源序列中的按指定元素的重复元素
public static IEnumerable Identical(this IEnumerable source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var setT = new HashSet();
foreach (var item in source)
{
if (!setT.Add(item))
{
yield return item;
}
}
}

///


/// 通过对指定的值进行比较返回序列中重复的元素
///

/// 序列中的数据类型
/// 指定的比较属性类型
/// 源数据
/// 应用于每个元素的转换函数
/// 一个包含源序列中的按指定元素的重复元素
public static IEnumerable Identical<T, TResult>(this IEnumerable source, Func<T, TResult> selector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (selector == null)
throw new ArgumentNullException(nameof(selector));
var setTResult = new HashSet();
foreach (var item in source)
{
var result = selector(item);
if (!setTResult.Add(result))
{
yield return item;
}
}
}

6. 使用注解对实体进行验证, 可用在除ASP.NET MVC之外的任一框架中(MVC已内置), 需引用System.ComponentModel.DataAnnotations类库及命名空间

///


/// 通过使用验证上下文和验证结果结合, 确定指定的对象是否有效
///

/// 要验证的对象
/// 用于包含每个失败的验证的集合
/// 如果对象有效,为 true, 否则为 false
public static bool TryValidate(this object instance, ICollection validationResults = null)
{
var context = new ValidationContext(instance);
return Validator.TryValidateObject(instance, context, validationResults, true);
}

///


/// 通过使用验证上下文和验证结果结合, 确定指定的方法参数是否有效
///

/// 要验证的方法所属对象
/// 类中要验证的方法名称
/// 方法参数的值
/// 用于包含每个失败的验证的集合
/// 如果参数有效,为 true, 否则为 false
public static bool TryValidate(this object instance, string methodName, object[] values, ICollection validationResults = null)
{
var typeOfInstance = instance.GetType();
var methodInfo = typeOfInstance.GetMethod(methodName, values.Select(p => p?.GetType() ?? typeof(object)).ToArray());
if (methodInfo == null)
{
//查找可能存在的泛型方法
methodInfo = typeOfInstance
.GetMethods()
.FirstOrDefault(p => p.Name == methodName
&& p.IsGenericMethod
&& p.GetParameters().Length == values.Length);
}
if (methodInfo == null)
throw new ArgumentException("找不到指定的方法,请检查方法名或者参数数组是否存在匹配");
var paramInfos = methodInfo.GetParameters();
bool isValid = true;
for (var i = 0; i < values.Length; i++)
{
var value = values[i];
var paramInfo = paramInfos.ElementAt(i);
var context = new ValidationContext(paramInfo);
context.DisplayName = paramInfo.Name;
var attrs = paramInfo.GetCustomAttributes();
isValid = isValid & Validator.TryValidateValue(value, context, validationResults, attrs);
}
return isValid;
}

///


/// 通过使用验证上下文和验证结果结合, 确定指定的方法参数是否有效
///

/// 要验证的方法所属对象
/// 类中要验证的方法名称
/// 方法参数值
public static void Validate(this object instance, string methodName, object[] values)
{
var typeOfInstance = instance.GetType();
var methodInfo = typeOfInstance.GetMethod(methodName, values.Select(p => p?.GetType() ?? typeof(object)).ToArray());
if (methodInfo == null)
{
//查找可能存在的泛型方法
methodInfo = typeOfInstance
.GetMethods()
.FirstOrDefault(p => p.Name == methodName
&& p.IsGenericMethod
&& p.GetParameters().Length == values.Length);
}
if (methodInfo == null)
throw new ArgumentException("找不到指定的方法,请检查方法名或者参数数组是否存在匹配");
var paramInfos = methodInfo.GetParameters();
for (var i = 0; i < values.Length; i++)
{
var value = values[i];
var paramInfo = paramInfos.ElementAt(i);
var context = new ValidationContext(paramInfo);
Validator.ValidateValue(value, context, paramInfo.GetCustomAttributes());
}
}

验证的目标实体:

public class User
{
[Required] //表示此字段不能为null或空字符串
[StringLength(3)] //表示此字段能接收的最大字符串长度
public string Name { get; set; }

[Range(0, 120)] //表示数据字段的最小最大范围
public int Age { get; set; }
}

实体验证:

User user = new User() { Name = "A,ning", Age = 140 };

ICollection errors = new List();

//如果不关注验证结果, 可不传入 errors 参数, 验证失败将只返回false
if (user.TryValidate(errors))
{
Console.WriteLine("验证通过");
}
else
{
Console.WriteLine("验证失败");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Join(Environment.NewLine, errors.Select(p => p.ErrorMessage)));
}

方法参数验证(支持泛型方法):

public void SayHello([Required][StringLength(3)] T name, [Range(0, 120)] int age)
{
var errors = new List();
if (this.TryValidate(nameof(SayHello), new object[] { name, age }, errors))
{
Console.WriteLine("验证通过");
}
else
{
Console.WriteLine("验证失败");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Join(Environment.NewLine, errors.Select(p => p.ErrorMessage)));
}
}

7. 判断Type是否为.NET Framework内置类型

///


/// 判断是否为用户自定义类型
///

/// type
/// 如果为 .NET 内置类型返回false,否则返回true
public static bool IsCustomType(this Type type)
{
return (type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
}

posted @ 2017-10-13 11:12  A_ning  阅读(1104)  评论(1)    收藏  举报