• 将枚举作为参数,迭代枚举。Passing Enum type as a parameter


    private void SelectNextCameraMode()
    {
        World.Camera.CameraMode = 
         (CameraActionMode)GetNextEnum<cameraactionmode>(World.Camera.CameraMode);
        UpdateCameraModeLabel();
    }
     
    private void SelectNextMouseMode()
    {
        this.mouseMode = (MouseMode)GetNextEnum<mousemode>(this.mouseMode);
        UpdateMouseModeLabel();
    }
     
    private Enum GetNextEnum<t>(object currentlySelectedEnum)
    {
        Type enumList = typeof(T);
        if (!enumList.IsEnum)
            throw new InvalidOperationException("Object is not an Enum.");
     
        Array enums = Enum.GetValues(enumList);
        int index = Array.IndexOf(enums, currentlySelectedEnum);
        index = (index + 1) % enums.Length;
        return (Enum)enums.GetValue(index);
    }
     
    private Enum GetPreviousEnum<t>(object currentlySelectedEnum)
    {
        Type enumList = typeof(T);
        if (!enumList.IsEnum)
            throw new InvalidOperationException("Object is not an Enum.");
     
        Array enums = Enum.GetValues(enumList);
        int index = Array.IndexOf(enums, currentlySelectedEnum);
        index = (((index == 0) ? enums.Length : index) - 1);
        return (Enum)enums.GetValue(index);
    } 
    

     本文参考http://www.codeproject.com/Tips/244647/Passing-Enum-type-as-a-parameter

      

    By:Smok.
  • 相关阅读:
    Linux
    python中元类
    POJ 1182 食物链
    POJ 1733 Parity game
    top 介绍
    周记 2014.8.31
    windows10配置python
    oracle执行update时卡死问题的解决办法
    An Easy Introduction to CUDA C and C++
    super()
  • 原文地址:https://www.cnblogs.com/ouyanga/p/2159123.html
Copyright © 2020-2023  润新知