提交 4acaef07 编写于 作者: T tanghai

增加EnumHelper.FromString方法

上级 8ab531a1
using System;
using System.Windows;
using System.Windows.Controls;
using Common.Helper;
using Model;
namespace Modules.BehaviorTreeModule
......@@ -58,7 +59,7 @@ namespace Modules.BehaviorTreeModule
return;
}
this.TreeNodeViewModel.Type =
(int) Enum.Parse(typeof (NodeType), this.cbType.SelectedValue.ToString().Trim());
(int) EnumHelper.FromString<NodeType>(this.cbType.SelectedValue.ToString().Trim());
}
}
}
\ No newline at end of file
......@@ -50,7 +50,7 @@ namespace Model
public void Register<T, R>(Func<T, R> func)
{
Opcode opcode = (Opcode) Enum.Parse(typeof (Opcode), typeof (T).Name);
Opcode opcode = EnumHelper.FromString<Opcode>(typeof (T).Name);
events.Add(opcode, messageBytes =>
{
T t = MongoHelper.FromBson<T>(messageBytes, 6);
......@@ -59,17 +59,6 @@ namespace Model
});
}
public void RegisterAsync<T, R>(Func<T, Task<R>> func)
{
Opcode opcode = (Opcode)Enum.Parse(typeof(Opcode), typeof(T).Name);
eventsAsync.Add(opcode, async messageBytes =>
{
T t = MongoHelper.FromBson<T>(messageBytes, 6);
R r = await func(t);
return MongoHelper.ToBson(r);
});
}
public async Task<byte[]> RunAsync(Opcode opcode, byte[] messageBytes)
{
Func<byte[], byte[]> func = null;
......
......@@ -169,7 +169,7 @@ namespace Model
++this.requestId;
byte[] requestBuffer = MongoHelper.ToBson(request);
Opcode opcode = (Opcode)Enum.Parse(typeof(Opcode), request.GetType().Name);
Opcode opcode = EnumHelper.FromString<Opcode>(request.GetType().Name);
byte[] opcodeBuffer = BitConverter.GetBytes((ushort)opcode);
byte[] idBuffer = BitConverter.GetBytes(this.requestId);
channel.SendAsync(new List<byte[]> { opcodeBuffer, idBuffer, requestBuffer });
......
using System.Threading.Tasks;
namespace Model
namespace Model
{
public interface IRegister
{
......@@ -16,14 +14,4 @@ namespace Model
public abstract R Run(T t);
}
public abstract class MEventAsync<T, R> : IRegister
{
public void Register()
{
World.Instance.GetComponent<MessageComponent>().RegisterAsync<T, R>(this.Run);
}
public abstract Task<R> Run(T t);
}
}
\ No newline at end of file
namespace Model
{
public enum Opcode: short
public enum Opcode: ushort
{
CMsgLogin = 1,
RpcResponse = 30000,
......
......@@ -19,23 +19,5 @@
}
return false;
}
public static bool IsRpcRequestMessage(Opcode opcode)
{
if ((ushort)opcode > 20000 && (ushort)opcode < 30000)
{
return true;
}
return false;
}
public static bool IsRpcResponseMessage(Opcode opcode)
{
if ((ushort)opcode > 30000 && (ushort)opcode < 40000)
{
return true;
}
return false;
}
}
}
......@@ -17,5 +17,10 @@ namespace Common.Helper
}
return -1;
}
public static T FromString<T>(string str)
{
return (T) Enum.Parse(typeof (T), str);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册