diff --git a/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs b/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs index 804b7c0..195175a 100644 --- a/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs +++ b/modules/JiShe.CollectBus.Analyzers/ComplexTypeSourceAnalyzers.cs @@ -162,13 +162,19 @@ namespace JiShe.CollectBus.IncrementalGenerator code.AppendLine($"namespace {classSymbol.ContainingNamespace.ToDisplayString()};"); code.AppendLine(); - // 处理泛型类型参数 + // 处理泛型类型名称 + var accessibility = classSymbol.DeclaredAccessibility switch + { + Accessibility.Public => "public", + _ => "internal" + }; + var genericParams = classSymbol.IsGenericType ? $"<{string.Join(", ", classSymbol.TypeParameters.Select(t => t.Name))}>" : ""; code.AppendLine( - $"public sealed class {classSymbol.Name}Accessor{genericParams} " + + $"{accessibility} sealed class {classSymbol.Name}Accessor{genericParams} " + // 保留泛型参数 $": ISourceEntityAccessor<{classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>"); code.AppendLine("{"); @@ -306,26 +312,32 @@ namespace JiShe.CollectBus.IncrementalGenerator public static class SourceEntityAccessorFactory { private static readonly ConcurrentDictionary _accessors = new(); - + public static ISourceEntityAccessor GetAccessor() { return (ISourceEntityAccessor)_accessors.GetOrAdd(typeof(T), t => { - var typeName = $"{t.Namespace}.{t.Name}Accessor"; - if (t.IsGenericType) - { - var genericArgs = t.GetGenericArguments(); - var genericDef = t.GetGenericTypeDefinition(); - typeName = $"{genericDef.Namespace}.{genericDef.Name}Accessor`{genericArgs.Length}"; - } - - var type = Type.GetType(typeName) - ?? Assembly.GetAssembly(t)?.GetType(typeName) + // 获取泛型类型定义信息(如果是泛型类型) + var isGeneric = t.IsGenericType; + var genericTypeDef = isGeneric ? t.GetGenericTypeDefinition() : null; + var arity = isGeneric ? genericTypeDef!.GetGenericArguments().Length : 0; + + // 构建访问器类名 + var typeName = isGeneric + ? $"{t.Namespace}.{genericTypeDef!.Name.Split('`')[0]}Accessor`{arity}" + : $"{t.Namespace}.{t.Name}Accessor"; + + // 尝试从当前程序集加载 + var accessorType = Assembly.GetAssembly(t)!.GetType(typeName) ?? throw new InvalidOperationException($"Accessor type {typeName} not found"); - - return t.IsGenericType - ? Activator.CreateInstance(type.MakeGenericType(t.GetGenericArguments())) - : Activator.CreateInstance(type); + + // 处理泛型参数 + if (isGeneric && accessorType.IsGenericTypeDefinition) + { + accessorType = accessorType.MakeGenericType(t.GetGenericArguments()); + } + + return Activator.CreateInstance(accessorType)!; }); } } diff --git a/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntityAccessor.cs b/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntityAccessor.cs new file mode 100644 index 0000000..9c9c773 --- /dev/null +++ b/modules/JiShe.CollectBus.IoTDB/Model/TableModelSingleMeasuringEntityAccessor.cs @@ -0,0 +1,77 @@ +// +#nullable enable +using System; +using System.Collections.Generic; +using JiShe.CollectBus.Analyzers.Shared; +namespace JiShe.CollectBus.IoTDB.Model; + +public sealed class TableModelSingleMeasuringEntityAccessor3 : ISourceEntityAccessor> +{ + public static string GetSingleColumn_Item1(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity obj) => obj.SingleColumn.Item1; + public static void SetSingleColumn_Item1(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity obj, string value) => obj.SingleColumn = (value, obj.SingleColumn.Item2); + public static T GetSingleColumn_Item2(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity obj) => obj.SingleColumn.Item2; + public static void SetSingleColumn_Item2(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity obj, T value) => obj.SingleColumn = (obj.SingleColumn.Item1, value); + public static string GetSystemName(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.SystemName; + public static void SetSystemName(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.SystemName = value; + public static string GetProjectId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.ProjectId; + public static void SetProjectId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.ProjectId = value; + public static string GetDeviceType(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.DeviceType; + public static void SetDeviceType(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.DeviceType = value; + public static string GetDeviceId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.DeviceId; + public static void SetDeviceId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.DeviceId = value; + public static long GetTimestamps(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.Timestamps; + public static void SetTimestamps(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, long value) => obj.Timestamps = value; + public List PropertyList { get; } = new List() + { +"SingleColumn.Item1","SingleColumn.Item2","SystemName","ProjectId","DeviceType","DeviceId","Timestamps" }; + public object GetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity targetEntity, string propertyName) + { + return propertyName switch + { + "SingleColumn.Item1" => GetSingleColumn_Item1(targetEntity), + "SingleColumn.Item2" => GetSingleColumn_Item2(targetEntity), + "SystemName" => GetSystemName(targetEntity), + "ProjectId" => GetProjectId(targetEntity), + "DeviceType" => GetDeviceType(targetEntity), + "DeviceId" => GetDeviceId(targetEntity), + "Timestamps" => GetTimestamps(targetEntity), + _ => throw new ArgumentException($"Unknown property: {propertyName}") + }; + } + public void SetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity targetEntity, string propertyName, object value) + { + switch (propertyName) + { + case "SingleColumn.Item1": + SetSingleColumn_Item1( + targetEntity, (string)value); + break; + case "SingleColumn.Item2": + SetSingleColumn_Item2( + targetEntity, (T)value); + break; + case "SystemName": + SetSystemName( + targetEntity, (string)value); + break; + case "ProjectId": + SetProjectId( + targetEntity, (string)value); + break; + case "DeviceType": + SetDeviceType( + targetEntity, (string)value); + break; + case "DeviceId": + SetDeviceId( + targetEntity, (string)value); + break; + case "Timestamps": + SetTimestamps( + targetEntity, (long)value); + break; + default: + throw new ArgumentException($"Unknown property: {propertyName}"); + } + } +} diff --git a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs index aebce66..704363c 100644 --- a/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs +++ b/modules/JiShe.CollectBus.IoTDB/Provider/IoTDBProvider.cs @@ -566,6 +566,8 @@ namespace JiShe.CollectBus.IoTDB.Provider //metadata.ColumnNames.Insert(0, "Timestamps"); //metadata.DataTypes.Insert(0, TSDataType.TIMESTAMP); + var accessor = SourceEntityAccessorFactory.GetAccessor(); + while (dataSet.HasNext() && results.Count < pageSize) { var record = dataSet.Next(); @@ -588,11 +590,14 @@ namespace JiShe.CollectBus.IoTDB.Provider if (measurement.ToLower().EndsWith("time")) { - typeof(T).GetProperty(measurement)?.SetValue(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds)); + //typeof(T).GetProperty(measurement)?.SetValue(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds)); + + accessor.SetPropertyValue(entity, measurement, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds)); } else { - typeof(T).GetProperty(measurement)?.SetValue(entity, tempValue); + accessor.SetPropertyValue(entity, measurement, tempValue); + //typeof(T).GetProperty(measurement)?.SetValue(entity, tempValue); } }