diff --git a/Modbus.Net/Modbus.Net/Helper/ExtendedMethodHelper.cs b/Modbus.Net/Modbus.Net/Helper/ExtendedMethodHelper.cs deleted file mode 100644 index 9f1beed..0000000 --- a/Modbus.Net/Modbus.Net/Helper/ExtendedMethodHelper.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace Modbus.Net -{ - /// - /// 获取扩展方法的类 - /// - public static class ExtendedMethodHelper - { - /// - /// 获取程序集中的所有扩展方法 - /// - /// 扩展方法的第一个参数类即扩展类 - /// 程序集 - /// - public static IEnumerable GetExtensionMethods(this Type extendedType, Assembly assembly) - { - var query = from type in assembly.GetTypes() - where type.IsSealed && !type.IsGenericType && !type.IsNested - from method in type.GetMethods(BindingFlags.Static - | BindingFlags.Public | BindingFlags.NonPublic) - where method.IsDefined(typeof(ExtensionAttribute), false) - where method.GetParameters()[0].ParameterType == extendedType - select method; - return query.ToList(); - } - } -} diff --git a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs index 4e4ef05..cfc366b 100644 --- a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs +++ b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs @@ -3,8 +3,6 @@ using Quartz.Impl; using Quartz.Impl.Matchers; using System; using System.Collections.Generic; -using System.Linq; -using System.Reflection; using System.Threading.Tasks; namespace Modbus.Net @@ -146,15 +144,13 @@ namespace Modbus.Net { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create>() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) .Build(); - Type methodType = typeof(TMachineMethod); job.JobDataMap.Put("DataType", machineDataType); job.JobDataMap.Put("Machine", machine); - job.JobDataMap.Put("MethodType", methodType); if (_parentJobKey != null) { @@ -316,14 +312,12 @@ namespace Modbus.Net { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create>() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) .Build(); - Type methodType = typeof(TMachineMethod); job.JobDataMap.Put("Machine", machine); - job.JobDataMap.Put("MethodType", methodType); var listener = _scheduler.ListenerManager.GetJobListener("Modbus.Net.DataQuery.Chain." + _trigger.Key.Name) as JobChainingJobListenerWithDataMap; if (listener == null) throw new NullReferenceException("Listener " + "Modbus.Net.DataQuery.Chain." + _trigger.Key.Name + " is null"); @@ -416,20 +410,17 @@ namespace Modbus.Net /// /// 获取数据任务 /// - public class MachineGetDataJob : IJob where TReturnUnit : struct + public class MachineGetDataJob : IJob where TMachineMethod : IMachineMethod where TReturnUnit : struct { /// public async Task Execute(IJobExecutionContext context) { object machine; object machineDataType; - object methodType; context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType); - context.JobDetail.JobDataMap.TryGetValue("MethodType", out methodType); - MethodInfo invokeGetGenericMethod = typeof(IMachineMethod).GetExtensionMethods(GetType().Assembly).First(p => p.Name == "InvokeGet").MakeGenericMethod((Type)methodType, typeof(Dictionary>)); - var values = await (Task>>>)invokeGetGenericMethod.Invoke(machine, new object[] { machine, new object[] { (MachineDataType)machineDataType } }); + var values = await ((IMachineMethod)machine).InvokeGet>>(new object[] { (MachineDataType)machineDataType }); context.JobDetail.JobDataMap.Put("Value", values); await context.Scheduler.AddJob(context.JobDetail, true, false); @@ -463,7 +454,7 @@ namespace Modbus.Net /// /// 写数据任务 /// - public class MachineSetDataJob : IJob where TReturnUnit : struct + public class MachineSetDataJob : IJob where TMachineMethod : IMachineMethod where TReturnUnit : struct { /// public async Task Execute(IJobExecutionContext context) @@ -472,12 +463,10 @@ namespace Modbus.Net object machineDataType; object values; object valuesSet; - object methodType; context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType); context.JobDetail.JobDataMap.TryGetValue("Value", out values); context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet); - context.JobDetail.JobDataMap.TryGetValue("MethodType", out methodType); if (valuesSet == null && values != null) { valuesSet = ((ReturnStruct>>)values).Datas.MapGetValuesToSetValues(); @@ -488,8 +477,7 @@ namespace Modbus.Net context.JobDetail.JobDataMap.Put("Success", false); return; } - MethodInfo invokeGetGenericMethod = typeof(IMachineMethod).GetExtensionMethods(GetType().Assembly).First(p => p.Name == "InvokeSet").MakeGenericMethod((Type)methodType, typeof(Dictionary)); - var success = await (Task>)invokeGetGenericMethod.Invoke(machine, new object[] { machine, new object[] { (MachineDataType)machineDataType }, (Dictionary)valuesSet }); + var success = await ((IMachineMethod)machine).InvokeSet>(new object[] { (MachineDataType)machineDataType }, (Dictionary)valuesSet); context.JobDetail.JobDataMap.Put("Success", success); } diff --git a/Samples/MachineJob/Worker.cs b/Samples/MachineJob/Worker.cs index 478f693..b31b32e 100644 --- a/Samples/MachineJob/Worker.cs +++ b/Samples/MachineJob/Worker.cs @@ -1,10 +1,10 @@ using Modbus.Net; using Modbus.Net.Modbus; using Modbus.Net.Siemens; -using ModbusMachine = Modbus.Net.Modbus.ModbusMachine; -using SiemensMachine = Modbus.Net.Siemens.SiemensMachine; -using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler; using DataReturnDef = Modbus.Net.DataReturnDef; +using ModbusMachine = Modbus.Net.Modbus.ModbusMachine; +using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler; +using SiemensMachine = Modbus.Net.Siemens.SiemensMachine; namespace MachineJob.Service {