示例#1
0
        private static void InitialiseObjectMessageHandlers()
        {
            var builder = ImmutableDictionary.CreateBuilder <ObjectOpcode, ObjectMessageHandler>();

            foreach (MethodInfo methodInfo in Assembly.GetExecutingAssembly()
                     .GetTypes()
                     .SelectMany(t => t.GetMethods()))
            {
                ObjectPacketHandlerAttribute attribute = methodInfo.GetCustomAttribute <ObjectPacketHandlerAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                ParameterInfo[] parameters = methodInfo.GetParameters();

                #region Debug
                Debug.Assert(parameters.Length == 2);
                Debug.Assert(typeof(NetworkSession).IsAssignableFrom(parameters[0].ParameterType));
                #endregion

                ParameterExpression sessionParameter = Expression.Parameter(typeof(NetworkSession));
                ParameterExpression messageParameter = Expression.Parameter(typeof(NetworkObject));

                MethodCallExpression methodCall          = Expression.Call(methodInfo, Expression.Convert(sessionParameter, parameters[0].ParameterType), messageParameter);
                Expression <ObjectMessageHandler> lambda = Expression.Lambda <ObjectMessageHandler>(methodCall, sessionParameter, messageParameter);

                builder.Add(attribute.Opcode, lambda.Compile());
            }

            objectMessageHandlers = builder.ToImmutable();
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        private static void InitialiseObjectPacketHandlers()
        {
            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                foreach (MethodInfo methodInfo in type.GetMethods())
                {
                    ObjectPacketHandlerAttribute attribute = methodInfo.GetCustomAttribute <ObjectPacketHandlerAttribute>();
                    if (attribute == null)
                    {
                        continue;
                    }

                    ParameterInfo[] parameters = methodInfo.GetParameters();

                    Debug.Assert(parameters.Length == 2);
                    Debug.Assert(typeof(NetworkSession).IsAssignableFrom(parameters[0].ParameterType));

                    ParameterExpression sessionParameter = Expression.Parameter(typeof(NetworkSession));
                    ParameterExpression objectParameter  = Expression.Parameter(typeof(NetworkObject));

                    MethodCallExpression             methodCall = Expression.Call(methodInfo, Expression.Convert(sessionParameter, parameters[0].ParameterType), objectParameter);
                    Expression <ObjectPacketHandler> lambda     = Expression.Lambda <ObjectPacketHandler>(methodCall, sessionParameter, objectParameter);

                    objectPacketHandlers.Add(attribute.Opcode, lambda.Compile());
                }
            }
        }