public object GetInstance(Type channel)
        {
            if (_existingChannels.ContainsKey(channel))
            {
                MethodInfo            initContext = channel.BaseType.GetProperty("Context").GetSetMethod(true);
                IChannelMethodContext context     = _contextProvider.GetDefaultContext();
                object[] mparam = new object[] { context };
                initContext.Invoke(_existingChannels[channel], mparam);
                return(_existingChannels[channel]);
            }

            ConstructorInfo ctor        = channel.GetConstructor(new Type[0]);
            string          methodName  = $"{channel.Name}Ctor";
            DynamicMethod   dynamicMtd  = new DynamicMethod(methodName, channel, new Type[0]);
            ILGenerator     iLGenerator = dynamicMtd.GetILGenerator();

            iLGenerator.Emit(OpCodes.Newobj, ctor);
            iLGenerator.Emit(OpCodes.Ret);

            ChannelObject channelObject  = (ChannelObject)dynamicMtd.CreateDelegate(typeof(ChannelObject));
            object        channelInstace = channelObject();

            PropertyInfo[] properties = channel.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.GetCustomAttribute <ImportedServiceAttribute>() != null)
                {
                    object service = _importResolver.GetImportedService(property.PropertyType);
                    property.SetValue(channelInstace, service);
                }
            }

            _existingChannels.Add(channel, channelInstace);
            return(channelInstace);
        }
Esempio n. 2
0
        public object GetInstance(Type channel)
        {
            //If channel is already instantiated its important to change old Context to current request Context
            if (_existingChannels.ContainsKey(channel))
            {
                MethodInfo            initContext = channel.BaseType.GetProperty("Context").GetSetMethod(true);
                IChannelMethodContext context     = _contextProvider.GetDefaultContext();
                object[] mparam = new object[] { context };
                initContext.Invoke(_existingChannels[channel], mparam);
                return(_existingChannels[channel]);
            }

            //From the performance perspective its much much faster to generate instance with IL than with Activator
            ConstructorInfo ctor        = channel.GetConstructor(new Type[0]);
            string          methodName  = $"{channel.Name}Ctor";
            DynamicMethod   dynamicMtd  = new DynamicMethod(methodName, channel, new Type[0]);
            ILGenerator     iLGenerator = dynamicMtd.GetILGenerator();

            iLGenerator.Emit(OpCodes.Newobj, ctor);
            iLGenerator.Emit(OpCodes.Ret);

            ChannelObject channelObject  = (ChannelObject)dynamicMtd.CreateDelegate(typeof(ChannelObject));
            object        channelInstace = channelObject();

            //Since channels can have services that have ImportedService attribute its crucial to find them
            //so that channels dont break when trying to invoke requested service
            PropertyInfo[] properties = channel.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.GetCustomAttribute <ImportedServiceAttribute>() != null)
                {
                    object service = _importResolver.GetImportedService(property.PropertyType);
                    property.SetValue(channelInstace, service);
                }
            }

            _existingChannels.Add(channel, channelInstace);
            return(channelInstace);
        }
Esempio n. 3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken        token = JToken.ReadFrom(reader);
            ChannelObject obj   = token.ToObject <ChannelObject>(serializer);

            switch (obj.type)
            {
            case Discord.Enums.ChannelType.GUILD_CATEGORY:
                return(token.ToObject <ChannelCategoryDescriptor>(serializer));

            case Discord.Enums.ChannelType.GUILD_TEXT:
                return(token.ToObject <TextChannelDescriptor>(serializer));

            case Discord.Enums.ChannelType.GUILD_VOICE:
                return(token.ToObject <VoiceChannelDescriptor>(serializer));

            case Discord.Enums.ChannelType.DM:
            case Discord.Enums.ChannelType.GROUP_DM:
                return(token.ToObject <PrivateChannelDescriptor>(serializer));

            default:
                throw new InvalidCastException($"Unknown channel type '{obj.type}'.");
            }
        }
 private void RunChannelStatusUpdate(Channel channel, ChannelObject args)
 {
     MessageBroker.RunActions($"VisiLED{channel}", args);
 }
Esempio n. 5
0
        /// <summary>
        /// 根据指定的资源类型创建一个资源对象
        /// </summary>
        /// <param name="objType">资源类型编码</param>
        /// <returns></returns>
        public static ConfigObject CreateObject(int objType)
        {
            ConfigObject configObject;

            switch (objType)
            {
            case S1110Consts.RESOURCE_MACHINE:
                configObject = new MachineObject();
                break;

            case S1110Consts.RESOURCE_LICENSESERVER:
                configObject = new LicenseServiceObject();
                break;

            case S1110Consts.RESOURCE_ALARMSERVER:
                configObject = new AlarmServerObject();
                break;

            case S1110Consts.RESOURCE_VOICESERVER:
                configObject = new VoiceServiceObject();
                break;

            case S1110Consts.RESOURCE_DATATRANSFERSERVER:
            case S1110Consts.RESOURCE_CTIHUBSERVER:
            case S1110Consts.RESOURCE_DBBRIDGE:
            case S1110Consts.RESOURCE_ALARMMONITOR:
            case S1110Consts.RESOURCE_SFTP:
            case S1110Consts.RESOURCE_SCREENSERVER:
            case S1110Consts.RESOURCE_ISASERVER:
            case S1110Consts.RESOURCE_CMSERVER:
            case S1110Consts.RESOURCE_KEYGENERATOR:
            case S1110Consts.RESOURCE_FILEOPERATOR:
            case S1110Consts.RESOURCE_SPEECHANALYSISPARAM:
            case S1110Consts.RESOURCE_RECOVERSERVER:
            case S1110Consts.RESOURCE_CAPTURESERVER:
                configObject = new ServiceObject();
                break;

            case S1110Consts.RESOURCE_STORAGEDEVICE:
                configObject = new StorageDeviceObject();
                break;

            case S1110Consts.RESOURCE_PBXDEVICE:
                configObject = new PBXDeviceObject();
                break;

            case S1110Consts.RESOURCE_CHANNEL:
                configObject = new VoiceChannelObject();
                break;

            case S1110Consts.RESOURCE_SCREENCHANNEL:
                configObject = new ChannelObject();
                break;

            case S1110Consts.RESOURCE_NETWORKCARD:
                configObject = new NetworkCardObject();
                break;

            case S1110Consts.RESOURCE_VOIPPROTOCAL:
                configObject = new VoipProtocalObject();
                break;

            case S1110Consts.RESOURCE_CONCURRENT:
                configObject = new ConcurrentObject();
                break;

            case S1110Consts.RESOURCE_CTICONNECTION:
                configObject = new CTIConnectionObject();
                break;

            case S1110Consts.RESOURCE_CTICONNECTIONGROUP:
                configObject = new CTIConnectionGroupObject();
                break;

            case S1110Consts.RESOURCE_CTICONNECTIONGROUPCOLLECTION:
                configObject = new CTIConnectionGroupCollectionObject();
                break;

            case S1110Consts.RESOURCE_DOWNLOADPARAM:
                configObject = new DownloadParamObject();
                break;

            case S1110Consts.RESOURCE_ALARMMONITORPARAM:
                configObject = new AlarmMonitorParamObject();
                break;

            case S1110Consts.RESOURCE_CTIDBBRIDGE:
                configObject = new CTIDBBServerObject();
                break;

            case S1110Consts.RESOURCE_ALARMSERVICE:
                configObject = new AlarmServiceObject();
                break;

            case S1110Consts.RESOURCE_ALARMPROCESS:
                configObject = new AlarmProcessObject();
                break;

            default:
                configObject = new ConfigObject();
                break;
            }
            configObject.ObjectType = objType;
            return(configObject);
        }
 private void UpdateDisplay(ChannelObject args)
 {
     GraphSegmentCode = args.SegmentCode;
     SetMode(args.Mode, false);
     SetPower(((uint)args.Mode & (uint)Mode.Strobe) == 0 ? args.Power : args.StrobePower);
 }