将枚举作为参数,迭代枚举。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.
原文地址:https://www.cnblogs.com/ouyanga/p/2159123.html