修改代码

This commit is contained in:
ChenYi 2025-05-07 17:27:37 +08:00
parent ff517664fe
commit c47ee94469
3 changed files with 55 additions and 6 deletions

View File

@ -157,6 +157,7 @@ namespace JiShe.CollectBus.IncrementalGenerator
code.AppendLine("// <auto-generated/>"); code.AppendLine("// <auto-generated/>");
code.AppendLine("#nullable enable"); code.AppendLine("#nullable enable");
code.AppendLine("using System;"); code.AppendLine("using System;");
code.AppendLine("using System.Reflection;");
code.AppendLine("using System.Collections.Generic;"); code.AppendLine("using System.Collections.Generic;");
code.AppendLine("using JiShe.CollectBus.Analyzers.Shared;"); code.AppendLine("using JiShe.CollectBus.Analyzers.Shared;");
code.AppendLine($"namespace {classSymbol.ContainingNamespace.ToDisplayString()};"); code.AppendLine($"namespace {classSymbol.ContainingNamespace.ToDisplayString()};");
@ -199,6 +200,13 @@ namespace JiShe.CollectBus.IncrementalGenerator
//生成当前类属性名称集合 //生成当前类属性名称集合
GeneratePropertyListForSourceEntity(propList, code, compilation, classSymbol); GeneratePropertyListForSourceEntity(propList, code, compilation, classSymbol);
//生成当前类属性信息集合
GeneratePropertyInfoListForSourceEntity(
propList,
code,
compilation,
classSymbol);
//生成当前类属性访问 //生成当前类属性访问
GetGeneratePropertyValueForSourceEntity( GetGeneratePropertyValueForSourceEntity(
@ -214,8 +222,6 @@ namespace JiShe.CollectBus.IncrementalGenerator
compilation, compilation,
classSymbol); classSymbol);
code.AppendLine("}"); code.AppendLine("}");
return code.ToString(); return code.ToString();
} }
@ -468,5 +474,42 @@ namespace JiShe.CollectBus.IncrementalGenerator
code.AppendLine(" };"); code.AppendLine(" };");
} }
/// <summary>
/// 生成当前类属性信息集合
/// </summary>
/// <param name="propList">属性集合</param>
/// <param name="code"></param>
/// <param name="compilation"></param>
/// <param name="classSymbol"></param>
private static void GeneratePropertyInfoListForSourceEntity(
IEnumerable<IPropertySymbol> propList,
StringBuilder code,
Compilation compilation,
INamedTypeSymbol classSymbol)
{
code.AppendLine(" public List<string> PropertyList {get;} = new List<string>()");
code.AppendLine(" {");
List<string> tempPropList = new List<string>();
foreach (var prop in propList)
{
if (prop.Type is INamedTypeSymbol { IsTupleType: true } tupleType)
{
foreach (var element in tupleType.TupleElements)
{
tempPropList.Add($"\"{prop.Name}.{element.Name}\"");
}
}
else
{
tempPropList.Add($"\"{prop.Name}\"");
}
}
code.Append(string.Join(",", tempPropList));
code.AppendLine(" };");
}
} }
} }

View File

@ -21,7 +21,7 @@ public sealed class TableModelSingleMeasuringEntityAccessor3<T> : ISourceEntityA
public static void SetDeviceId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.DeviceId = value; 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 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 static void SetTimestamps(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, long value) => obj.Timestamps = value;
public List<string> PropertyList { get; } = new List<string>() public List<string> PropertyNameList { get; } = new List<string>()
{ {
"SingleColumn.Item1","SingleColumn.Item2","SystemName","ProjectId","DeviceType","DeviceId","Timestamps" }; "SingleColumn.Item1","SingleColumn.Item2","SystemName","ProjectId","DeviceType","DeviceId","Timestamps" };
public object GetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> targetEntity, string propertyName) public object GetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> targetEntity, string propertyName)

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using System.Text; using System.Text;
namespace JiShe.CollectBus.Analyzers.Shared namespace JiShe.CollectBus.Analyzers.Shared
@ -25,6 +26,11 @@ namespace JiShe.CollectBus.Analyzers.Shared
/// <summary> /// <summary>
/// 属性名称集合 /// 属性名称集合
/// </summary> /// </summary>
List<string> PropertyList { get; } List<string> PropertyNameList { get; }
/// <summary>
/// 属性信息集合
/// </summary>
List<PropertyInfo> PropertyInfoList { get; }
} }
} }