//BaseWorkItem item=null;
//switch (type)
//{// case Consts.WorkType.Cancel:
// OnCancel(c);
// return item;
// case Consts.WorkType.Cut:
// item = new CutWorkItem();
// break;
// case Consts.WorkType.Hunting:
// item = new HuntWorkItem();
// break;
// case Consts.WorkType.Plant:
// item = new PlantWorkItem();
// break;
// case Consts.WorkType.Construct:
// item = new ConstructWorkItem();
// break;
// case Consts.WorkType.Carry:
// item = new CarryWorkItem();
// break;
// case Consts.WorkType.Dismantle:
// item = new DismantleWorkItem();
// break;
//}
/// <summary>
/// 数组顺需要和工作项枚举顺序一致
/// </summary>
public static Type[] types = new Type[] {null, null,typeof(WalkWorkItem), null,null,typeof(HuntWorkItem), typeof(CutWorkItem),
typeof(PlantWorkItem),null, typeof(CarryWorkItem), typeof(ConstructWorkItem), null, typeof(EatWorkItem),
typeof(SleepWorkItem), typeof(DismantleWorkItem),null, typeof(StrikeBackWorkItem) };
var type = WorkItemCreator.types[(int)workType];
BaseWorkItem item = (BaseWorkItem)System.Activator.CreateInstance(type);
在工厂类中 可以使用Activator.CreateInstance(type) 来减少代码量 使代码更优雅。