Beispiel #1
0
        public static string Get_Method_Header(CodeClassInfo codeClassInfo)
        {
            var str = string.Format(@"
        public static {0} Read(byte[] buffer,int offset)
        {{
            var msg = new {0}();", codeClassInfo.ClassName);

            return(str);
        }
Beispiel #2
0
        private static string Get_Class(CodeClassInfo codeClassInfo)
        {
            var str = Get_Class_Header(codeClassInfo);

            str += Get_WriteMethod(codeClassInfo);
            str += Get_ReadMethod(codeClassInfo);
            str += Get_Class_Foot();
            return(str);
        }
Beispiel #3
0
        private static string Get_Class_Header(CodeClassInfo codeClassInfo)
        {
            var str = String.Format(@"
using ByteBuffer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace {1}
{{
     public partial class {0}
     {{", codeClassInfo.ClassName, codeClassInfo.NameSpace);

            return(str);
        }
Beispiel #4
0
        private static string Get_ReadMethod(CodeClassInfo codeClassInfo)
        {
            var str = CodeReadHelper.Get_Method_Header(codeClassInfo);

            foreach (var info in codeClassInfo.MemberList)
            {
                switch (info.ByteType)
                {
                case ByteType.Bool:
                    str += CodeReadHelper.GetBool(info);
                    break;

                case ByteType.Int8:
                    str += CodeReadHelper.GetInt8(info);
                    break;

                case ByteType.UInt8:
                    str += CodeReadHelper.GetUInt8(info);
                    break;

                case ByteType.Int16:
                    str += CodeReadHelper.GetInt16(info.Name);
                    break;

                case ByteType.UInt16:
                    str += CodeReadHelper.GetUInt16(info.Name);
                    break;

                case ByteType.Int32:
                    str += CodeReadHelper.GetInt32(info.Name);
                    break;

                case ByteType.UInt32:
                    str += CodeReadHelper.GetUInt32(info.Name);
                    break;

                case ByteType.Float32:
                    str += CodeReadHelper.GetFloat32(info.Name);
                    break;

                case ByteType.Int64:
                    str += CodeReadHelper.GetInt64(info.Name);
                    break;

                case ByteType.String:
                    str += CodeReadHelper.GetString(info.Name);
                    break;

                case ByteType.Object:
                    str += CodeReadHelper.GetObject(info);
                    break;

                //arrary
                case ByteType.BoolArray:
                    str += CodeReadHelper.GetArrayBool(info);
                    break;

                case ByteType.Int8Array:
                    str += CodeReadHelper.GetArrayInt8(info);
                    break;

                case ByteType.UInt8Array:
                    str += CodeReadHelper.GetArrayUInt8(info);
                    break;

                case ByteType.Int16Array:
                    str += CodeReadHelper.GetArrayInt16(info.Name);
                    break;

                case ByteType.UInt16Array:
                    str += CodeReadHelper.GetArrayUInt16(info.Name);
                    break;

                case ByteType.Int32Array:
                    str += CodeReadHelper.GetArrayInt32(info.Name);
                    break;

                case ByteType.UInt32Array:
                    str += CodeReadHelper.GetArrayUInt32(info.Name);
                    break;

                case ByteType.Float32Array:
                    str += CodeReadHelper.GetArrayFloat32(info.Name);
                    break;

                case ByteType.Float64Array:
                    str += CodeReadHelper.GetArrayFloat64(info.Name);
                    break;

                case ByteType.StringArray:
                    str += CodeReadHelper.GetArrayString(info.Name);
                    break;

                case ByteType.ObjectArray:
                    str += CodeReadHelper.GetArrayObject(info);
                    break;

                //新增
                case ByteType.Int24:
                    str += CodeReadHelper.GetInt24(info.Name);
                    break;

                case ByteType.UInt24:
                    str += CodeReadHelper.GetUInt24(info.Name);
                    break;

                case ByteType.Int24Array:
                    str += CodeReadHelper.GetInt24Array(info.Name);
                    break;

                case ByteType.UInt24Array:
                    str += CodeReadHelper.GetUInt24Array(info.Name);
                    break;

                default:
                    throw new Exception("类型错误");
                }
            }
            str += CodeReadHelper.Get_Method_Foot();
            return(str);
        }
Beispiel #5
0
        /// <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);
            }
        }