Browse Source

review

tags/v2.6.2
yedajiang44 2 years ago
parent
commit
342a30bc7c
1 changed files with 36 additions and 18 deletions
  1. +36
    -18
      src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs

+ 36
- 18
src/JT808.Protocol/Internal/JT808_0x8900_Custom_Factory.cs View File

@@ -15,35 +15,53 @@ namespace JT808.Protocol.Internal

public void Register(Assembly externalAssembly)
{
foreach (var type in externalAssembly.GetTypes().Where(w => w.GetInterface(nameof(JT808_0x8900_BodyBase)) == typeof(JT808_0x8900_BodyBase)))
foreach (var item in externalAssembly.GetTypes().Where(x => x.GetConstructor(Type.EmptyTypes) != default))
{
var instance = Activator.CreateInstance(type);
var attachid = (byte)type.GetProperty(nameof(JT808_0x0200_CustomBodyBase.AttachInfoId)).GetValue(instance);
if (Map.ContainsKey(attachid))
{
throw new ArgumentException($"{type.FullName} {attachid} An element with the same key already exists.");
}
else
{
Map.Add(attachid, instance);
}
Register(item);
}
}

public IJT808_0x8900_Custom_Factory SetMap<TJT808_0x8900_Custom_Factory>() where TJT808_0x8900_Custom_Factory : JT808_0x8900_BodyBase
public IJT808_0x8900_Custom_Factory SetMap<T>()
{
Type type = typeof(TJT808_0x8900_Custom_Factory);
var instance = Activator.CreateInstance(type);
var passthroughType = (byte)type.GetProperty(nameof(JT808_0x8900_BodyBase.PassthroughType)).GetValue(instance);
if (Map.ContainsKey(passthroughType))
var type = typeof(T);
if (type.GetConstructor(Type.EmptyTypes) != default)
{
throw new ArgumentException($"{type.FullName} {passthroughType} An element with the same key already exists.");
Register(type);
}
else
{
Map.Add(passthroughType, instance);
throw new ArgumentException($"{type.FullName} must be parameterless constructor.");
}
return this;
}

void Register(Type type)
{
var instance = Activator.CreateInstance(type);
if (instance is JT808_0x0200_CustomBodyBase jT808_0X0200_Custom)
{
var key = jT808_0X0200_Custom.AttachInfoId;
if (Map.ContainsKey(key))
{
throw new ArgumentException($"{type.FullName} {key} An element with the same key already exists.");
}
else
{
Map.Add(key, instance);
}
}
else if (instance is JT808_0x8900_BodyBase jT808_0X8900_Custom)
{
var key = jT808_0X8900_Custom.PassthroughType;
if (Map.ContainsKey(key))
{
throw new ArgumentException($"{type.FullName} {key} An element with the same key already exists.");
}
else
{
Map.Add(key, instance);
}
}
}
}
}

Loading…
Cancel
Save