diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs index c2df660..19ebbef 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs @@ -1,9 +1,8 @@ -using JT808.Protocol.Extensions; -using JT808.Protocol.Interfaces; +using System.Reflection; +using JT808.Protocol.Extensions; using JT808.Protocol.Internal; using JT808.Protocol.MessageBody; using JT808.Protocol.Test.MessageBody.JT808_0X8900_BodiesImpl; -using System.Reflection; using Xunit; namespace JT808.Protocol.Test.MessageBody @@ -51,5 +50,31 @@ namespace JT808.Protocol.Test.MessageBody byte[] bytes = "0B0000303901".ToHexBytes(); string json = JT808Serializer.Analyze(bytes); } + + [Fact] + public void Test4() + { + var config = new DefaultGlobalConfig(GetType().Name+nameof(Test4)); + config.JT808_0x8900_Custom_Factory.Register(Assembly.GetExecutingAssembly()); + var serializer = config.GetSerializer(); + var jT808_0X8900 = new JT808_0x8900 + { + PassthroughType = 0x0B, + JT808_0X8900_BodyBase = new JT808_0X8900_Test_BodiesImpl + { + Id = 12345, + Sex = 0x01 + } + }; + string hex = serializer.Serialize(jT808_0X8900).ToHexString(); + Assert.Equal("0B0000303901", hex); + byte[] bytes = hex.ToHexBytes(); + + jT808_0X8900 = serializer.Deserialize(bytes); + JT808_0X8900_Test_BodiesImpl jT808_0X8900_Test_BodiesImpl = (JT808_0X8900_Test_BodiesImpl)jT808_0X8900.JT808_0X8900_BodyBase; + Assert.Equal(0x0B, jT808_0X8900.PassthroughType); + Assert.Equal((uint)12345, jT808_0X8900_Test_BodiesImpl.Id); + Assert.Equal(0x01, jT808_0X8900_Test_BodiesImpl.Sex); + } } } diff --git a/src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs b/src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs index 5ac0429..c75f96b 100644 --- a/src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs +++ b/src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs @@ -15,7 +15,8 @@ namespace JT808.Protocol.Internal public void Register(Assembly externalAssembly) { - foreach (var item in externalAssembly.GetTypes().Where(x => x is JT808_0x8900_BodyBase && x.GetConstructor(Type.EmptyTypes) != default)) + var baseType = typeof(JT808_0x8900_BodyBase); + foreach (var item in externalAssembly.GetTypes().Where(x => baseType.IsAssignableFrom(x) && x.GetConstructor(Type.EmptyTypes) != default)) { Register(item); }