using System;
using System.Reflection;
namespace DA_Assets.Tools
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class ClearAttribute : Attribute { }
public static class ObjectCleaner
{
///
/// Cleans serialized and other fields or properties of an object marked with the attribute.
/// This is especially useful for objects generated from Figma layouts, which produce a substantial amount of data.
/// Cleaning these objects helps reduce memory usage and removes unnecessary serialized data from the Inspector.
///
/// The attribute type used to identify fields or properties to be cleared.
/// The object whose fields or properties will be cleared.
public static void ClearByAttribute(object obj) where T : Attribute
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
Type type = obj.GetType();
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var field in fields)
{
if (Attribute.IsDefined(field, typeof(T)))
{
SetMemberValue(field, obj);
}
}
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var property in properties)
{
if (Attribute.IsDefined(property, typeof(T)))
{
if (property.CanWrite)
{
SetMemberValue(property, obj);
}
}
}
}
private static void SetMemberValue(MemberInfo member, object obj)
{
Type memberType;
Action