public static string GetArrayInt8(CodeMemberInfo memberInfo) { //var length = this.{ 0} == null ? 0 : this.{0}.Count; //buffer[offset] = (byte)length; //offset++; //foreach (var id in this.{0}) //{ // buffer[offset] = (byte)id; // offset++; //} var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var id in this.{0}) {{ buffer[offset] = (byte)id; offset++; }}}}", memberInfo.Name); return(str); }
public static string GetArrayUInt32(CodeMemberInfo memberInfo) { //foreach (var _byte in BitConverter.GetBytes(Convert.ToUInt32(12))) //{ // buffer[offset] = _byte; // offset += 1; //} var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var item in this.{0}) {{ foreach (var _byte in BitConverter.GetBytes(Convert.ToUInt32(item)))) {{ buffer[offset] = _byte; offset ++; }} }}}}", memberInfo.Name); return(str); }
public static string GetObject(CodeMemberInfo info) { //var roleLength = buffer[offset]; //offset++; //if (roleLength == 0) //{ // msg.Role = null; //} //else //{ // var _buffer = new ArraySegment<byte>(buffer, offset, roleLength).ToArray(); // msg.Role = Role.Read(_buffer, 0); // offset += roleLength; //} var str = string.Format(@" var {0}Length = buffer[offset]; offset++; if ({0}Length == 0) {{ msg.{0} = null; }} else {{ var _buffer = new ArraySegment<byte>(buffer, offset, {0}Length).ToArray(); msg.{0} = {1}.Read(_buffer, 0); offset += {0}Length; }}", info.Name, info.TypeName); return(str); }
public static string GetBool(CodeMemberInfo memberInfo) { var str = string.Format(@" buffer[offset] =(byte)(this.{0}? 1 : 0); offset += 1;", memberInfo.Name); return(str); }
public static string GetUInt8(CodeMemberInfo info) { var str = string.Format(@" msg.{0}=({1})buffer[offset]; offset++;", info.Name, info.TypeName); return(str); }
public static string GetBool(CodeMemberInfo info) { var str = string.Format(@" msg.{0}=buffer[offset]==1; offset++; ", info.Name, info.TypeName); return(str); }
public static string GetArrayObject(CodeMemberInfo memberInfo) { //var countRoleList = this.RoleList == null ? 0 : this.RoleList.Count; //buffer[offset] = (byte)countRoleList; //offset++; //if (countRoleList > 0) //{ // foreach (var item in this.RoleList) // { // if (item == null) // { // buffer[offset] = 0; // offset += 1; // } // else // { // var _buffer = item.Write(); // buffer[offset] = (byte)_buffer.Length; // offset++; // for (var i = 0; i < _buffer.Length; i++) // { // buffer[offset] = _buffer[i]; // offset++; // } // } // } //} var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var item in this.{0}) {{ if (item == null) {{ buffer[offset] = 0; offset ++; }} else {{ var _buffer = item.Write(); buffer[offset] = (byte)_buffer.Length; offset++; for (var i = 0; i < _buffer.Length; i++) {{ buffer[offset] = _buffer[i]; offset++; }} }} }} }}", memberInfo.Name); return(str); }
public static string GetInt64(CodeMemberInfo memberInfo) { var str = string.Format(@" foreach (var _byte in BitConverter.GetBytes(this.{0})) {{ buffer[offset] = _byte; offset += 1; }} ", memberInfo.Name); return(str); }
public static string GetUInt8(CodeMemberInfo memberInfo) { //buffer[offset] = Convert.ToSByte(memberInfo.Name); ; //offset += 1; var str = string.Format(@" buffer[offset] = Convert.ToByte(this.{0}); offset += 1;", memberInfo.Name); return(str); }
public static string GetUInt24(CodeMemberInfo memberInfo) { var str = string.Format(@" var {0}Bytes = UInt24.GetBytes(this.{0}); foreach (var _byte in {0}Bytes) {{ buffer[offset] = _byte; offset += 1; }}", memberInfo.Name); return(str); }
public static string GetString(CodeMemberInfo memberInfo) { var str = string.Format(@" var {0}Bytes = StringEncoding.GetBytes(this.{0}); buffer[offset] = (byte){0}Bytes.Length; offset += 1; foreach (var _byte in {0}Bytes) {{ buffer[offset] = _byte; offset += 1; }}", memberInfo.Name); return(str); }
public static string GetArrayUInt8(CodeMemberInfo info) { var str = string.Format(@" var count{0} = buffer[offset]; offset++; var list{0} = new List<byte>(); for (var i = 0; i < count{0}; i++) {{ var item = buffer[offset]; offset++; list{0}.Add(item); }} msg.{0} = list{0};", info.Name); return(str); }
public static string GetArrayObject(CodeMemberInfo memberInfo) { //var countRoleList = buffer[offset]; //offset++; //var listRoleList = new List<Role>(); //for (var i = 0; i < countRoleList; i++) //{ // var _roleLength = buffer[offset]; // offset++; // if (_roleLength == 0) // { // listRoleList.Add(null); // } // else // { // var _buffer = new ArraySegment<byte>(buffer, offset, _roleLength).ToArray(); // listRoleList.Add(Role.Read(_buffer, 0)); // offset += _roleLength; // } //} //msg.RoleList = listRoleList; var str = string.Format(@" var count{0} = buffer[offset]; offset++; var list{1}List = new List<{1}>(); for (var i = 0; i < count{0}; i++) {{ var _{1}Length = buffer[offset]; offset++; if (_{1}Length == 0) {{ list{1}List.Add(null); }} else {{ var _buffer = new ArraySegment<byte>(buffer, offset, _{1}Length).ToArray(); list{1}List.Add({1}.Read(_buffer, 0)); offset += _{1}Length; }} }} msg.{0} = list{1}List;", memberInfo.Name, memberInfo.TypeName); return(str); }
public static string GetArrayBool(CodeMemberInfo memberInfo) { var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var item in this.{0}) {{ buffer[offset] = (byte)(item?1:0); offset++; }} }}", memberInfo.Name); return(str); }
public static string GetUInt32(CodeMemberInfo memberInfo) { //foreach (var _byte in BitConverter.GetBytes(Convert.ToUInt32(12))) //{ // buffer[offset] = _byte; // offset += 1; //} var str = string.Format(@" foreach (var _byte in BitConverter.GetBytes(Convert.ToUInt32(this.{0}))) {{ buffer[offset] = _byte; offset += 1; }} ", memberInfo.Name); return(str); }
public static string GetUInt24Array(CodeMemberInfo memberInfo) { var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var item in this.{0}) {{ foreach (var _byte in UInt24.GetBytes(item)) {{ buffer[offset] = _byte; offset ++; }} }}}}", memberInfo.Name); return(str); }
public static string GetObject(CodeMemberInfo memberInfo) { //if (this.Role == null) //{ // buffer[offset] = 0; // offset += 1; //} //else //{ // var _buffer = this.Role.Write(); // buffer[offset] = (byte)_buffer.Length; // offset++; // for (var i = 0; i < _buffer.Length; i++) // { // buffer[offset] = _buffer[i]; // offset++; // } //} var str = string.Format(@" if (this.{0} == null) {{ buffer[offset] = 0; offset += 1; }} else {{ var _buffer = this.{0}.Write(); buffer[offset] = (byte)_buffer.Length; offset++; for (var i = 0; i < _buffer.Length; i++) {{ buffer[offset] = _buffer[i]; offset++; }} }}", memberInfo.Name); return(str); }
public static string GetArrayString(CodeMemberInfo memberInfo) { var str = string.Format(@" var count{0} = this.{0} == null ? 0 : this.{0}.Count; buffer[offset] = (byte)count{0}; offset++; if (count{0} > 0) {{ foreach (var item in this.{0}) {{ var nameBytes = StringEncoding.GetBytes(item); buffer[offset] = (byte)nameBytes.Length; offset += 1; foreach (var _byte in nameBytes) {{ buffer[offset] = _byte; offset += 1; }} }}}}", memberInfo.Name); return(str); }
/// <summary> /// 生产code /// </summary> /// <param name="assmble">当前的 Assembly.GetExecutingAssembly();</param> public static void Run(Assembly assmble) { var projectPath = Path.GetFullPath("../../.."); //var assmble = Assembly.GetExecutingAssembly(); var modules = assmble.GetModules(); var types = new List <Type>(); foreach (var module in modules) { var _types = module.GetTypes().Where(i => i.GetCustomAttribute <BtyeContract>() != null).ToList(); types.AddRange(_types); } foreach (var type in types) { var codeClassInfo = new CodeClassInfo(); codeClassInfo.ClassName = type.Name; codeClassInfo.NameSpace = type.Namespace; codeClassInfo.MemberList = new List <CodeMemberInfo>(); var propertyes = type.GetRuntimeProperties().Where(i => i.GetCustomAttribute <ByteMember>() != null); foreach (var property in propertyes) { var menberInfo = new CodeMemberInfo(); var byteMember = property.GetCustomAttribute <ByteMember>(); menberInfo.Name = property.Name; menberInfo.ByteType = byteMember.ByteType; menberInfo.Order = byteMember.Order; if (byteMember.TypeName == null) { menberInfo.TypeName = property.PropertyType.Name; } else { menberInfo.TypeName = byteMember.TypeName; } codeClassInfo.MemberList.Add(menberInfo); } codeClassInfo.MemberList = codeClassInfo.MemberList.OrderBy(i => i.Order).ToList(); var nameSpaces = codeClassInfo.NameSpace.Split("."); var inerPath = string.Empty; for (var i = 1; i < nameSpaces.Length; i++) { inerPath += "\\" + nameSpaces[i]; } var fileDir = projectPath + inerPath + @"\Generate\"; if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } var fileName = projectPath + inerPath + @"\Generate\" + codeClassInfo.ClassName + ".cs"; //if (File.Exists(fileName)) continue; var fileStream = File.Create(fileName); fileStream.Close(); var codeStr = Get_Class(codeClassInfo); File.WriteAllText(fileName, codeStr, Encoding.UTF8); } }