コード例 #1
0
        /// <summary>注册可被解析的消息类型</summary>
        /// <param name="messageTypeID">可被解析的消息类型ID</param>
        /// <param name="messageType"><paramref name="messageTypeID"/>对应的<see cref="ProtoBuf.IExtensible"/>类型</param>
        /// <remarks>对泛型重载Register&lt;T&gt;的非泛型包装</remarks>
        public void Register(MessageTypeID messageTypeID, Type messageType)
        {
            BindingFlags flags  = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
            MethodInfo   method = this.GetType().GetMethod("Register", flags, null, new Type[] { typeof(MessageTypeID) }, null);

            method = method.MakeGenericMethod(messageType);
            method.Invoke(this, new object[] { messageTypeID });
        }
コード例 #2
0
        /// <summary>注册可被解析的消息类型</summary>
        /// <typeparam name="T">可被解析的消息类型ID</typeparam>
        /// <param name="messageType"><typeparamref name="T"/>对应的<see cref="ProtoBuf.IExtensible"/>类型</param>
        public void Register <T>(MessageTypeID messageType) where T : class
        {
            // 反序列化预编译
            ProtoBuf.Serializer.PrepareSerializer <T>();

            // 注册
            messageTypeTable[typeof(T)]   = messageType;
            deserializeTable[messageType] = (stream) => ProtoBuf.Serializer.Deserialize <T>(stream);
        }