Essgee.Unity/Assets/Plugins/Essgee/Extensions/GeneralExtensionMethods.cs
2025-01-02 17:55:16 +08:00

38 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace Essgee.Extensions
{
public static class GeneralExtensionMethods
{
// https://www.c-sharpcorner.com/UploadFile/ff2f08/deep-copy-of-object-in-C-Sharp/
public static T CloneObject<T>(this T source)
{
var type = source.GetType();
var target = (T)Activator.CreateInstance(type);
foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (property.CanWrite)
{
if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(typeof(string)) || property.PropertyType.Equals(typeof(Type)))
property.SetValue(target, property.GetValue(source, null), null);
else
{
object objPropertyValue = property.GetValue(source, null);
if (objPropertyValue == null)
property.SetValue(target, null, null);
else
property.SetValue(target, objPropertyValue.CloneObject(), null);
}
}
}
return target;
}
}
}