Routine update

This commit is contained in:
yubaolee
2015-11-23 18:15:29 +08:00
parent e4f4c03389
commit e4305be038
5 changed files with 79 additions and 19 deletions

View File

@@ -60,6 +60,7 @@
<Compile Include="JsonConverter.cs" />
<Compile Include="JsonHelper.cs" />
<Compile Include="LogHelper.cs" />
<Compile Include="ObjectHelper.cs" />
<Compile Include="PredicateBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Response.cs" />

View File

@@ -0,0 +1,62 @@
// ***********************************************************************
// Assembly : Infrastructure
// Author : Yubao Li
// Created : 11-23-2015
//
// Last Modified By : Yubao Li
// Last Modified On : 11-23-2015
// ***********************************************************************
// <copyright file="ObjectHelper.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>对象COPY/初始化帮助</summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Infrastructure
{
public static class ObjectHelper
{
public static void CopyTo<T>(this object source, T target)
where T : class,new()
{
if (source == null)
return;
if (target == null)
{
target = new T();
}
foreach (var property in target.GetType().GetProperties())
{
var propertyValue = source.GetType().GetProperty(property.Name).GetValue(source, null);
if (propertyValue != null)
{
if (propertyValue.GetType().IsClass)
{
}
target.GetType().InvokeMember(property.Name, BindingFlags.SetProperty, null, target, new object[] { propertyValue });
}
}
foreach (var field in target.GetType().GetFields())
{
var fieldValue = source.GetType().GetField(field.Name).GetValue(source);
if (fieldValue != null)
{
target.GetType().InvokeMember(field.Name, BindingFlags.SetField, null, target, new object[] { fieldValue });
}
}
}
}
}