c# - 匹配 2 个具有相同实例名称的不同对象

标签 c# .net

我想知道是否可以将对象与其实例名称匹配。

我得到了:

class AnimatedEntity : DrawableEntity
{
    Animation BL { get; set; }
    Animation BR { get; set; }
    Animation TL { get; set; }
    Animation TR { get; set; }
    Animation T { get; set; }
    Animation R { get; set; }
    Animation L { get; set; }
    Animation B { get; set; }

    Orientation orientation ;

    public virtual int Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        //draw depends on orientation
    }
}

enum Orientation { 
    SE, SO, NE, NO, 
    N , E, O, S, 
    BL, BR, TL, TR, 
    T, R, L, B 
}

Orientation 是一个枚举,Animation 是一个类。

我可以使用相同的名称从 Orientation 调用正确的动画吗?

最佳答案

与其将动画存储在属性中,不如使用字典怎么样?

Dictionary<Orientation, Animation> anim = new Dictionary<Orientation, Animation> {
    { Orientation.BL, blAnimation },
    { Orientation.BR, brAnimation },
    { Orientation.TL, tlAnimation },
    { Orientation.TR, trAnimation },
    { Orientation.T, tAnimation },
    { Orientation.R, rAnimation },
    { Orientation.L, lAnimation },
    { Orientation.B, bAnimation }
};

然后您可以使用 anim[orientation] 访问适当的动画。

关于c# - 匹配 2 个具有相同实例名称的不同对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148204/

相关文章:

c# - 在 T-SQL 中将 '01-Sep-2017' 转换为 '01/09/2017'?

c# - 使用C#创建的excel文件保存时出现冲突,如何关闭提示对话框?

c# - 我应该在 MVC 编码中使用什么标准

c# - mbunit v3 文档

c# - LINQ to Entities 无法识别该方法并且无法将此方法转换为存储表达式

.net - Linq SqlMethods.Like 失败

c# - Visual Studio 更新不应构建的项目二进制文件

.net - 3 层开发的最佳实践

c# - 多对多映射未创建正确的表

c# - 如何创建递归函数来复制所有文件和文件夹