update REFLECTION_DISABLED

This commit is contained in:
Mikhail 2024-08-07 09:45:34 +08:00
parent 108fb6c41b
commit f4df0d07a1
2 changed files with 51 additions and 21 deletions

View File

@ -1,12 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
using System.Reflection; using System.Reflection;
#endif
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public static class EcsDebugUtility public static class EcsDebugUtility
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
private const BindingFlags RFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private const BindingFlags RFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
#endif
#region GetGenericTypeName #region GetGenericTypeName
public static string GetGenericTypeFullName<T>(int maxDepth = 2) public static string GetGenericTypeFullName<T>(int maxDepth = 2)
@ -15,7 +19,7 @@ namespace DCFApixels.DragonECS
} }
public static string GetGenericTypeFullName(Type type, int maxDepth = 2) public static string GetGenericTypeFullName(Type type, int maxDepth = 2)
{ {
return GetGenericTypeNameInternal(type, maxDepth, true); return GetGenericTypeName_Internal(type, maxDepth, true);
} }
public static string GetGenericTypeName<T>(int maxDepth = 2) public static string GetGenericTypeName<T>(int maxDepth = 2)
{ {
@ -23,10 +27,11 @@ namespace DCFApixels.DragonECS
} }
public static string GetGenericTypeName(Type type, int maxDepth = 2) public static string GetGenericTypeName(Type type, int maxDepth = 2)
{ {
return GetGenericTypeNameInternal(type, maxDepth, false); return GetGenericTypeName_Internal(type, maxDepth, false);
} }
private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull) private static string GetGenericTypeName_Internal(Type type, int maxDepth, bool isFull)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
string typeName = isFull ? type.FullName : type.Name; string typeName = isFull ? type.FullName : type.Name;
if (!type.IsGenericType || maxDepth == 0) if (!type.IsGenericType || maxDepth == 0)
{ {
@ -47,6 +52,10 @@ namespace DCFApixels.DragonECS
genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}"); genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}");
} }
return $"{typeName}<{genericParams}>"; return $"{typeName}<{genericParams}>";
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(GetGenericTypeName_Internal)} method does not work.");
return isFull ? type.FullName : type.Name;
#endif
} }
#endregion #endregion
@ -56,10 +65,11 @@ namespace DCFApixels.DragonECS
{ {
return AutoToString(self, typeof(T), isWriteName); return AutoToString(self, typeof(T), isWriteName);
} }
//TODO сделать специальный вывод в виде названий констант для Enum-ов
private static string AutoToString(object target, Type type, bool isWriteName) private static string AutoToString(object target, Type type, bool isWriteName)
{ {
#if !REFLECTION_DISABLED #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
//TODO сделать специальный вывод в виде названий констант для Enum-ов
#pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations. #pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
var fields = type.GetFields(RFL_FLAGS); var fields = type.GetFields(RFL_FLAGS);
#pragma warning restore IL2070 #pragma warning restore IL2070
@ -283,7 +293,7 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region TypeMetaSource #region TypeMetaProvider
public static bool IsTypeMetaProvided(object obj) public static bool IsTypeMetaProvided(object obj)
{ {
return obj is IEcsTypeMetaProvider; return obj is IEcsTypeMetaProvider;

View File

@ -3,7 +3,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
using System.Reflection; using System.Reflection;
#endif
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
@ -81,14 +83,7 @@ namespace DCFApixels.DragonECS
if (_initFlags.HasFlag(InitFlag.Name) == false) if (_initFlags.HasFlag(InitFlag.Name) == false)
{ {
(_name, _isCustomName) = MetaGenerator.GetMetaName(_type); (_name, _isCustomName) = MetaGenerator.GetMetaName(_type);
if (_isCustomName) _typeName = _isCustomName ? MetaGenerator.GetTypeName(_type) : _name;
{
_typeName = MetaGenerator.GetTypeName(_type);
}
else
{
_typeName = _name;
}
_initFlags |= InitFlag.Name; _initFlags |= InitFlag.Name;
} }
} }
@ -301,12 +296,12 @@ namespace DCFApixels.DragonECS
//private static HashSet<Type> _; //private static HashSet<Type> _;
#region GetMemberType //#region GetMemberType
public static EcsMemberType GetMemberType(Type type) //public static EcsMemberType GetMemberType(Type type)
{ //{
throw new NotImplementedException(); // throw new NotImplementedException();
} //}
#endregion //#endregion
#region GetMetaName/GetTypeName #region GetMetaName/GetTypeName
public static string GetTypeName(Type type) public static string GetTypeName(Type type)
@ -315,6 +310,7 @@ namespace DCFApixels.DragonECS
} }
public static (string, bool) GetMetaName(Type type) public static (string, bool) GetMetaName(Type type)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false; bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false;
if (isCustom) if (isCustom)
{ {
@ -332,6 +328,10 @@ namespace DCFApixels.DragonECS
return ($"{atr.name}<{genericParams}>", isCustom); return ($"{atr.name}<{genericParams}>", isCustom);
} }
return (EcsDebugUtility.GetGenericTypeName(type, GENERIC_NAME_DEPTH), isCustom); return (EcsDebugUtility.GetGenericTypeName(type, GENERIC_NAME_DEPTH), isCustom);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetMetaName)} method does not work.");
return (type.Name, false);
#endif
} }
#endregion #endregion
@ -342,35 +342,55 @@ namespace DCFApixels.DragonECS
} }
public static (MetaColor, bool) GetColor(Type type) public static (MetaColor, bool) GetColor(Type type)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
bool isCustom = type.TryGetCustomAttribute(out MetaColorAttribute atr); bool isCustom = type.TryGetCustomAttribute(out MetaColorAttribute atr);
return (isCustom ? atr.color : AutoColor(type), isCustom); return (isCustom ? atr.color : AutoColor(type), isCustom);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetColor)} method does not work.");
return (AutoColor(type), false);
#endif
} }
#endregion #endregion
#region GetGroup #region GetGroup
public static MetaGroup GetGroup(Type type) public static MetaGroup GetGroup(Type type)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
return type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty; return type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty;
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetGroup)} method does not work.");
return MetaGroup.Empty;
#endif
} }
#endregion #endregion
#region GetDescription #region GetDescription
public static MetaDescription GetDescription(Type type) public static MetaDescription GetDescription(Type type)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
bool isCustom = type.TryGetCustomAttribute(out MetaDescriptionAttribute atr); bool isCustom = type.TryGetCustomAttribute(out MetaDescriptionAttribute atr);
return isCustom ? atr.Data : MetaDescription.Empty; return isCustom ? atr.Data : MetaDescription.Empty;
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetDescription)} method does not work.");
return MetaDescription.Empty;
#endif
} }
#endregion #endregion
#region GetTags #region GetTags
public static IReadOnlyList<string> GetTags(Type type) public static IReadOnlyList<string> GetTags(Type type)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает
var atr = type.GetCustomAttribute<MetaTagsAttribute>(); var atr = type.GetCustomAttribute<MetaTagsAttribute>();
return atr != null ? atr.Tags : Array.Empty<string>(); return atr != null ? atr.Tags : Array.Empty<string>();
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work.");
return Array.Empty<string>();
#endif
} }
#endregion #endregion
} }
#endregion #endregion
} }
public enum EcsMemberType : byte public enum EcsMemberType : byte