将枚举作为参数,迭代枚举。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

 

 

 

  

posted @ 2011-08-30 09:25  xgdw  阅读(1032)  评论(0编辑  收藏  举报