iotgateway/WalkingTec.Mvvm/WalkingTec.Mvvm.Core/Extensions/SystemExtensions/SystemExtension.cs
2021-12-14 14:10:44 +08:00

68 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Reflection;
namespace WalkingTec.Mvvm.Core.Extensions
{
/// <summary>
/// System Extension
/// </summary>
public static class SystemExtension
{
#region Guid Extensions
public static string ToNoSplitString(this Guid self)
{
return self.ToString().Replace("-", string.Empty);
}
#endregion
/// <summary>
/// 将CrudVM中Entity的关联字段设为空并返回一个新的CrudVM
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static object GetCleanCrudVM(this object self)
{
var mtype = self.GetType();
if(typeof(IBaseCRUDVM<TopBasePoco>).IsAssignableFrom(mtype))
{
var rv = mtype.GetConstructor(Type.EmptyTypes).Invoke(null);
var toppros = mtype.GetAllProperties();
foreach (var tpro in toppros)
{
if(tpro.Name == "Entity")
{
var entity = tpro.GetValue(self);
var pros = tpro.PropertyType.GetAllProperties();
var newEntity = tpro.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(null);
bool isBasePoco = typeof(IBasePoco).IsAssignableFrom(tpro.PropertyType);
//将所有TopBasePoco的属性赋空值防止添加关联的重复内容
foreach (var pro in pros)
{
if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco)) == false)
{
if (isBasePoco == false || (pro.Name != "UpdateTime" && pro.Name != "UpdateBy"))
{
pro.SetValue(newEntity, pro.GetValue(entity));
}
}
}
tpro.SetValue(rv, newEntity);
}
else
{
if (tpro.CanWrite)
{
tpro.SetValue(rv, tpro.GetValue(self));
}
}
}
return rv;
}
return null;
}
}
}