我會走這條路:
首先,你需要一種枚舉的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ConsoleApp1
{
public class MyEnumeration
{
#region Private Fields
private readonly string _displayName;
private readonly int _value;
private readonly int _interval;
private readonly Action _action;
#endregion Private Fields
#region Protected Constructors
protected MyEnumeration()
{
}
protected MyEnumeration(int value, string displayName, int interval, Action action)
{
_value = value;
_displayName = displayName;
_interval = interval;
_action = action;
}
#endregion Protected Constructors
#region Public Properties
public string DisplayName
{
get { return _displayName; }
}
public int Value
{
get { return _value; }
}
public int Interval
{
get { return _interval; }
}
public Action Action
{
get { return _action; }
}
#endregion Public Properties
#region Public Methods
public static int AbsoluteDifference(MyEnumeration firstValue, MyEnumeration secondValue)
{
var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
return absoluteDifference;
}
public static T FromDisplayName<T>(string displayName) where T : MyEnumeration, new()
{
var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName);
return matchingItem;
}
public static T FromValue<T>(int value) where T : MyEnumeration, new()
{
var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
return matchingItem;
}
public static IEnumerable<T> GetAll<T>() where T : MyEnumeration, new()
{
var type = typeof(T);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = new T();
var locatedValue = info.GetValue(instance) as T;
if (locatedValue != null)
{
yield return locatedValue;
}
}
}
public int CompareTo(object other)
{
return Value.CompareTo(((MyEnumeration)other).Value);
}
public override bool Equals(object obj)
{
var otherValue = obj as MyEnumeration;
if (otherValue == null)
{
return false;
}
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = _value.Equals(otherValue.Value);
return typeMatches && valueMatches;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public override string ToString()
{
return DisplayName;
}
#endregion Public Methods
#region Private Methods
private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : MyEnumeration, new()
{
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
if (matchingItem == null)
{
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
throw new ApplicationException(message);
}
return matchingItem;
}
#endregion Private Methods
}
}
然後你可以創建您自己的枚舉類似於:
using System;
namespace ConsoleApp1
{
internal class LevelEnum : MyEnumeration
{
public static readonly LevelEnum Level1 = new LevelEnum(1, "Level 1", 2000, Program.Level1);
public static readonly LevelEnum Level2 = new LevelEnum(2, "Level 2", 3000, Program.Level2);
public static readonly LevelEnum Level3 = new LevelEnum(3, "Level 3", 4000, Program.Level3);
public static readonly LevelEnum Level4 = new LevelEnum(4, "Level 4", 5000, Program.Level4);
public static readonly LevelEnum Level5 = new LevelEnum(5, "Level 5", 6000, Program.Level5);
public static readonly LevelEnum Level6 = new LevelEnum(6, "Level 6", 7000, Program.Level6);
public static readonly LevelEnum Level7 = new LevelEnum(7, "Level 7", 8000, Program.Level7);
public static readonly LevelEnum Level8 = new LevelEnum(8, "Level 8", 9000, Program.Level8);
public LevelEnum()
{
}
protected LevelEnum(int value, string displayName, int interval, Action action) : base(value, displayName, interval, action)
{
}
}
}
,你可以用它喜歡:
private static void Main(string[] args)
{
int level = 5;
LevelEnum levelEnum = MyEnumeration.FromValue<LevelEnum>(level);
levelDisplay.Text = levelEnum.DisplayName;
timer = new Timer();
timer.Interval = levelEnum.Interval;
timer.Enabled = true;
timer.Elapsed += levelEnum.Action;
timer.Start();
}
internal static void Level1()
{
// Action for Level 1
}
internal static void Level2()
{
// Action for Level 2
}
internal static void Level3()
{
// Action for Level 3
}
有關使用開關的情況下怎麼辦? –
僅供參考,設置'timer.Enabled = true;'與'timer.Start();'相同。請參閱:[Timer.Enabled屬性文檔](https://msdn.microsoft.com/en-us/library/system.timers.timer.enabled(v = vs.110).aspx) –
@RufusL不知道那個,謝謝。 – Eli