32. 代码优化

1.代码优化


1.代码优化

csharp 复制代码
基于枚举创建不同对象

1).根据工厂和字典模式

public static class BuffEffectFactory
{
    // 字典:枚举 -> 对象构造委托
    private static readonly Dictionary<BuffEffect, Func<BuffEffectBase>> _effectCreators = new();

    static BuffEffectFactory()
    {
        // 预注册所有Buff类型
        _effectCreators.Add(BuffEffect.RebuildCard, () => new RebuildBuffEffect());
        _effectCreators.Add(BuffEffect.ControlDice, () => new ControlDiceBuffEffect());
        _effectCreators.Add(BuffEffect.BuildLvVariation, () => new BuildLvBuffEffect());
        _effectCreators.Add(BuffEffect.AddBuffByCondition, () => new AddBuffEffect());
        _effectCreators.Add(BuffEffect.DirectionChange, () => new DirectionChangeBuffEffect());
    }

    public static BuffEffectBase Create(BuffEffect effectType)
    {
        if (_effectCreators.TryGetValue(effectType, out var creator))
        {
            return creator();
        }
        throw new ArgumentOutOfRangeException(nameof(effectType), $"未注册的Buff类型:{effectType}");
    }
}

csharp 复制代码
2).通过特性和反射的方式

// 1. 定义特性:标记类对应的枚举值
[AttributeUsage(AttributeTargets.Class)]
public class BuffEffectAttribute : Attribute
{
    public BuffEffect EffectType { get; }
    public BuffEffectAttribute(BuffEffect effectType) => EffectType = effectType;
}

// 2. 给Buff类标记特性
[BuffEffect(BuffEffect.RebuildCard)]
public class RebuildBuffEffect : BuffEffectBase { }

[BuffEffect(BuffEffect.ControlDice)]
public class ControlDiceBuffEffect : BuffEffectBase { }
csharp 复制代码
static BuffEffectFactory()
{
    // 优化1:只扫描指定程序集(避免遍历所有程序集,减少扫描量)
    var targetAssembly = Assembly.GetExecutingAssembly(); // 假设Buff类都在当前程序集
    var buffTypes = targetAssembly.GetTypes()
        .Where(type => typeof(BuffEffectBase).IsAssignableFrom(type) 
                     && !type.IsAbstract 
                     && type.GetCustomAttribute<BuffEffectAttribute>() != null);

    foreach (var type in buffTypes)
    {
        var attribute = type.GetCustomAttribute<BuffEffectAttribute>();
        if (attribute == null) continue;

        // 优化2:缓存构造函数委托,替代Activator.CreateInstance
        var constructor = type.GetConstructor(Type.EmptyTypes); // 无参构造
        if (constructor == null)
        {
            throw new InvalidOperationException($"{type.Name} 必须提供无参构造函数");
        }
        // 将构造函数转为委托,调用时无反射损耗
        var creator = (Func<BuffEffectBase>)Delegate.CreateDelegate(
            typeof(Func<BuffEffectBase>), constructor);
        
        _effectCreators.Add(attribute.EffectType, creator);
    }
}
相关推荐
Sator13 小时前
unity解决粒子与物体接触时的硬边缘问题
unity·游戏引擎
程序员JerrySUN7 小时前
Jetson边缘嵌入式实战课程第三讲:L4T 与 Jetson 系统架构
linux·服务器·人工智能·安全·unity·系统架构·游戏引擎
萌萌的提莫队长8 小时前
Unity HDRP 渲染管线 Camera 输出到RenderTexture没有Alpha通道
unity·游戏引擎
winlife_9 小时前
Unity Editor 工具不该用反射写组件字段:SerializedObject 在 4 个场景里非用不可
unity·自动化·游戏引擎
星河耀银海9 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
郝学胜-神的一滴10 小时前
中级OpenGL教程 005:为球体&平面注入法线灵魂
c++·unity·图形渲染·three.js·opengl·unreal
那个村的李富贵10 小时前
unity编辑器工具,输出使用的字体
unity·编辑器·游戏引擎
游乐码1 天前
UnityGUI(五)GUI控件综合使用
开发语言·unity·c#
LF男男1 天前
TshitBullect.cs
unity
游乐码1 天前
Unity(十六)切换场景及鼠标相关
unity·游戏引擎