/// <summary>
/// 复制对象成员
/// </summary>
/// <param name="source">被复制的对象</param>
/// <param name="target">要复制的对象</param>
/// <param name="members">需要复制的成员</param>
/// <param name="ignoreMembers">忽略复制的成员</param>
public virtual void CopyMembers(object source, object target, string[] members, string[] ignoreMembers)
{
// 获取对象类型
Type type = source.GetType();
// 获取对象属性信息
PropertyInfo[] propertyInfos = type.GetProperties();
// 遍历复制对象属性
foreach (PropertyInfo propertyInfo in propertyInfos)
{
// 获取当前属性名
string name = propertyInfo.Name;
// 判断当前属性是否需要进行复制
if ((members == null || members.Length <= 0 || members.Contains("*") || members.Contains(name)) && (ignoreMembers == null || ignoreMembers.Length <= 0 || (!ignoreMembers.Contains("*") && !ignoreMembers.Contains(name))))
{
// 判断当前属性是否可读写
if (propertyInfo.CanRead && propertyInfo.CanWrite)
{
// 获取当前属性的GET访问器
MethodInfo getMethodInfo = propertyInfo.GetSetMethod();
// 获取当前属性的SET访问器
MethodInfo setMethodInfo = propertyInfo.GetSetMethod();
// 判断GET/SET访问器是否是公开的
if (getMethodInfo.IsPublic && setMethodInfo.IsPublic)
{
object value;
// 判断GET访问器是否是静态访问器
if (getMethodInfo.IsStatic)
{
// 获取当前属性的静态值
value = propertyInfo.GetValue(null, null);
}
else
{
// 获取被复制对象当前属性的实例值
value = propertyInfo.GetValue(source, null);
}
// 判断SET访问器是否是静态访问器
if (setMethodInfo.IsStatic)
{
// 设置当前属性的静态值
propertyInfo.SetValue(null, value, null);
}
else
{
// 设置要复制对象当前属性的实例值
propertyInfo.SetValue(target, value, null);
}
}
}
}
}
// 获取对象字段信息
FieldInfo[] fieldInfos = type.GetFields();
// 遍历复制对象字段
foreach (FieldInfo fieldInfo in fieldInfos)
{
// 获取当前字段名
string name = fieldInfo.Name;
// 判断当前字段是否需要进行复制
if ((members == null || members.Length <= 0 || members.Contains("*") || members.Contains(name)) && (ignoreMembers == null || ignoreMembers.Length <= 0 || (!ignoreMembers.Contains("*") && !ignoreMembers.Contains(name))))
{
// 判断当前字段是否是公开可写的
if (fieldInfo.IsPublic && !fieldInfo.IsLiteral && !fieldInfo.IsInitOnly)
{
// 判断当前字段是否是静态的
if (fieldInfo.IsStatic)
{
// 获取当前字段的静态值
object value = fieldInfo.GetValue(null);
// 设置当前字段的静态值
fieldInfo.SetValue(null, value);
}
else
{
// 获取被复制对象当前字段的实例值
object value = fieldInfo.GetValue(source);
// 设置要复制对象当前字段的实例值
fieldInfo.SetValue(target, value);
}
}
}
}
}
分类: 程序开发
程序员的自我修养
关于C#中通用对象类型转换的说明
/// <summary>
/// 转换对象类型
/// </summary>
/// <param name="targetType">转换后类型</param>
/// <param name="source">要转换的对象</param>
/// <returns>返回转换后的对象</returns>
public virtual object ConvertType(object source, Type targetType)
{
// 判断原始对象是否为空或者目标类型是否可以由原始对象直接转换
if (source == null || targetType.IsAssignableFrom(source.GetType()))
{
// 返回原始对象
return source;
}
else
{
// 判断原始对象是否是空字符串
if (source is string && string.IsNullOrWhiteSpace(source as string))
{
// 无法转换,返回默认值
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
}
else
{
// 判断目标对象是否是可空的泛型值类型
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
// 取出可空泛型值类型中的基础值类型
targetType = Nullable.GetUnderlyingType(targetType);
}
// 判断目标对象是否是枚举
if (targetType.IsEnum)
{
// 转换并返回目标对象枚举
return Enum.Parse(targetType, source.ToString());
}
else
{
// 判断原始对象是否是集合
if (source is ICollection)
{
// 将原始对象转换为集合对象
ICollection collection = source as ICollection;
// 判断目标类型是否是数组
if (targetType.IsArray)
{
// 根据目标类型和原始集合对象的长度创建目标对象数组
Array target = Activator.CreateInstance(targetType, collection.Count) as Array;
// 获取目标对象数组的元素类型
Type elementType = targetType.GetElementType();
// 遍历并转换原始集合对象数据到目标对象数组中
int i = 0;
foreach (var item in collection)
{
// 将当前原始数据项转换为目标元素类型并设置到对应的目标对象数组中
target.SetValue(this.ConvertType(item, elementType), i);
i++;
}
// 返回目标对象数组
return target;
}
else
{
// 判断目标类型是否是链表
if (typeof(IList).IsAssignableFrom(targetType))
{
// 根据目标类型创建目标对象链表
IList target = Activator.CreateInstance(targetType) as IList;
// 获取目标对象链表的元素类型
Type elementType = targetType.GetElementType();
// 遍历并转换原始集合对象数据到目标对象链表中
foreach (var item in collection)
{
// 将当前原始数据项转换为目标元素类型并添加到对应的目标对象链表中
target.Add(this.ConvertType(item, elementType));
}
// 返回目标对象链表
return target;
}
}
}
}
// 转换并返回目标对象
return Convert.ChangeType(source, targetType);
}
}
}
关于Java Spring MVC中快速获取HttpServletRequest和HttpServletResponse的说明
在Spring MVC中,为了能够随时取到当前请求的对象数据,可以通过@Autowired注解法获取,或者通过RequestContextHolder的静态方法getRequestAttributes获取相关变量,从而获取HttpServletRequest和HttpServletResponse对象。
一、获取HttpServletRequest:
// 注解法 @Autowired protected HttpServletRequest request; // RequestContextHolder法 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getResponse();
二、获取HttpServletResponse:
// 注解法 @Autowired protected HttpServletResponse response; // RequestContextHolder法 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletResponse response = attributes.getResponse();
关于Java中数组和链表排序的说明
一、数组排序:
Arrays.sort(array, new Comparator<String>() {
// 定义排序比较方法
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
});
二、链表排序:
Collections.sort(list, new Comparator<String>() {
// 定义排序比较方法
@Override
public int compare(String str1, String str2) {
return str2.compareTo(str1);
}
});
关于Java中去除字符串中HTML标记的说明
我们可以通过正则表达式匹配HTML标签,然后将其去除:
// 去除HTML标记
s = s.replaceAll("</?[^>]+>", "");
// 去除空格、回车、换行符、制表符
s = s.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
return s;