namespace NSExt.Extensions;
public static class GenericExtensions
{
///
/// 从指定的对象拷贝属性
///
/// 对象类型
/// 拷贝目标
/// 拷贝来源
/// 需要处理的属性名
/// True包含,false排除
public static void CopyFrom(this T me,
T copyObj,
IList propNameList = null,
bool isIncludeOrExclude = false)
{
foreach (var p in me.GetType().GetProperties()) {
if (!p.CanWrite) continue;
bool isSet;
if (isIncludeOrExclude)
isSet = propNameList?.Contains(p.Name) ?? false;
else
isSet = !propNameList?.Contains(p.Name) ?? true;
if (isSet) p.SetValue(me, copyObj.GetType().GetProperty(p.Name)?.GetValue(copyObj, null), null);
}
}
///
/// 判断是否与某对象相等
///
///
///
///
///
///
public static T Is(this T me, T compare, T ret) where T : struct
{
return me.Equals(compare) ? ret : me;
}
}