实现了添加设备的功能,并写了Object对象的扩展CopyTo方法,实现了数据表的创建。
This commit is contained in:
28
Extensions/ObjectExtensions.cs
Normal file
28
Extensions/ObjectExtensions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace PMSWPF.Extensions;
|
||||
|
||||
public static class ObjectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象转换,将source对象上的所有属性的值,都转换到target对象上
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void CopyTo<T>(this Object source ,T target)
|
||||
{
|
||||
var sourceType = source.GetType();
|
||||
var targetType = target.GetType();
|
||||
var sourceProperties = sourceType.GetProperties();
|
||||
foreach (PropertyInfo sourceProperty in sourceProperties)
|
||||
{
|
||||
PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
|
||||
if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType)
|
||||
{
|
||||
object value = sourceProperty.GetValue(source, null);
|
||||
targetProperty.SetValue(target, value, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user