예제 #1
0
        private static void InitializeCommandResponseCreators()
        {
            var assembly = Assembly.GetExecutingAssembly();
            var modules  = assembly.GetModules();
            var module   = modules.FirstOrDefault();

            if (module != null)
            {
                foreach (var type in module.GetTypes())
                {
                    if (!type.IsAbstract && !type.IsEnum &&
                        type.GetCustomAttributes(typeof(QcdmCommandAttribute), true).FirstOrDefault() is
                        QcdmCommandAttribute attribute)
                    {
                        var command = attribute.Command;
                        if (command != QcdmCommand.SubsysCmd)
                        {
                            var methodInfo = type.GetMethod("Parse",
                                                            BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);
                            if (methodInfo != null)
                            {
                                var fun = new QcdmCommandResponseCreatorFun(data =>
                                {
                                    try
                                    {
                                        return((IQcdmCommandResponse)methodInfo.Invoke(null, new object[]
                                        {
                                            data
                                        }));
                                    }
                                    catch (Exception e)
                                    {
                                        if (Logger != null)
                                        {
                                            Logger.LogError(e.Message);
                                        }
                                    }

                                    return(null);
                                });
                                var key = (uint)command;
                                _commandResponseCreators[key] = fun;
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private static void InitializeSubSystemResponseCreators()
        {
            var assembly = Assembly.GetExecutingAssembly();

            foreach (var type in assembly.GetTypes())
            {
                if (!type.IsAbstract && !type.IsEnum &&
                    type.GetCustomAttributes(typeof(QcdmSubSystemCommandAttribute), true).FirstOrDefault() is
                    QcdmSubSystemCommandAttribute attribute)
                {
                    var command   = BitConverter.GetBytes(attribute.Command);
                    var subSystem = attribute.SubSystem;
                    var bytes     = new[]
                    {
                        (byte)QcdmCommand.SubsysCmd, (byte)subSystem, command[0], command[1]
                    };
                    var key        = BitConverter.ToUInt32(bytes, 0);
                    var methodInfo = type.GetMethod("Parse",
                                                    BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);
                    if (methodInfo != null)
                    {
                        var fun = new QcdmCommandResponseCreatorFun(data =>
                        {
                            try
                            {
                                return((IQcdmCommandResponse)methodInfo.Invoke(null, new object[]
                                {
                                    data
                                }));
                            }
                            catch (Exception e)
                            {
                                if (Logger != null)
                                {
                                    Logger.LogError(e.Message);
                                }
                            }

                            return(null);
                        });
                        _commandResponseCreators[key] = fun;
                    }
                }
            }
        }