Net.Like.Xue.Tokyo/Assets/BITKit/Core/Attributes/IBITCommand.cs

99 lines
3.0 KiB
C#

using System;
using System.Reflection;
namespace BITKit
{
[AttributeUsage(AttributeTargets.Method)]
public class BITCommandAttribute : Attribute
{
}
/// <summary>
/// 自动注入依赖
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class InjectAttribute : Attribute
{
public static void Clear(object obj)
{
var fields = obj.GetType().GetFields(ReflectionHelper.Flags);
foreach (var field in fields)
{
if (field.GetCustomAttributes(typeof(InjectAttribute), true).Length > 0)
{
field.SetValue(obj, null);
}
}
}
public readonly bool CanBeNull;
public InjectAttribute()
{
}
public InjectAttribute(bool canBeNull)
{
CanBeNull = canBeNull;
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
public class CustomTypeAttribute : System.Attribute
{
public readonly Type Type;
public readonly bool AsGlobal;
public CustomTypeAttribute(Type type)
{
Type = type;
}
public CustomTypeAttribute(Type type, bool asGlobal)
{
Type = type;
AsGlobal = asGlobal;
}
public CustomTypeAttribute(bool asGlobal)
{
AsGlobal = asGlobal;
}
}
#if !Godot
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ExportAttribute : System.Attribute
{
public static string GetName(object obj)
{
var att = obj.GetType().GetCustomAttribute<ExportAttribute>();
att = obj switch
{
FieldInfo field => field.GetCustomAttribute<ExportAttribute>(),
MethodInfo method => method.GetCustomAttribute<ExportAttribute>(),
PropertyInfo property => property.GetCustomAttribute<ExportAttribute>(),
_ => att
};
if (att is null) return obj.GetType().Name;
if (string.IsNullOrEmpty(att.Name))
{
return obj switch
{
FieldInfo field => field.Name,
MethodInfo method => method.Name,
PropertyInfo property => property.Name,
_ => att.Name
};
}
return att.Name;
}
public readonly string Name;
public readonly IExportSetting Settings;
public ExportAttribute(){}
public ExportAttribute(IExportSetting settings)
{
Settings = settings;
}
public ExportAttribute(string name)
{
Name = name;
}
}
public interface IExportSetting{}
#endif
}