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);
    }
}
相关推荐
叶帆14 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君14 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子14 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
心前阳光14 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光14 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯15 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案15 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔15 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
玉夏15 天前
【Shader基础】UV 与纹理采样 Part1
unity·着色器·uv
zdr尽职尽责15 天前
Unity录像功能
学习·ui·unity·游戏引擎