Files
MsAddIns/MSDevTool/Helpers/SnoopHelpers.cs

93 lines
3.4 KiB
C#
Raw Normal View History

2026-02-28 21:01:57 +08:00
using System.Collections.Generic;
using System.Linq;
using Bentley.DgnPlatformNET.DgnEC;
using Bentley.DgnPlatformNET.Elements;
using Bentley.ECObjects.Instance;
using Bentley.ECObjects.Schema;
namespace MSDevTool.Helpers
{
internal class SnoopHelpers
{
/// <summary>
/// 获取元素的所有属性
/// </summary>
/// <param name="element"></param>
public static List<ECPropValue> GetECProperties(Element element)
{
if (null == element)
{
return null;
}
var props = new List<ECPropValue>();
DgnECManager ecManager = DgnECManager.Manager;
int count = 0;
DgnECInstanceCollection instCol = ecManager.GetElementProperties(element, ECQueryProcessFlags.SearchAllClasses);
foreach (IDgnECInstance inst in instCol)
{
count++;
//sb.AppendLine($"----ECInstance{count}[{inst.ClassDefinition.Name}]");
var propertyEnum = inst.ClassDefinition.GetEnumerator();
while (propertyEnum.MoveNext())
{
var cur = propertyEnum.Current;
IECPropertyValue propertyValue = inst.GetPropertyValue(cur.Name);
if (propertyValue.IsArray)
{
IECArrayValue arrayVal = propertyValue as IECArrayValue;
if (arrayVal.Count >= 1)
propertyValue = arrayVal[0];
}
var propName = cur.Name;
ECNameValidation.DecodeFromValidName(ref propName);
if (propertyValue.TryGetNativeValue(out var obj))
{
ECPropValue property = new()
{
ECInstanceName = $"EC类别定义-{inst.ClassDefinition.DisplayLabel}",
ECPropertyValue = propertyValue,
LabelName = propertyValue.Property.DisplayLabel,
PropName = propName,
PropValue = obj,
DataType = obj.GetType()
};
props.Add(property);
}
}
}
//MessageBox.Show(sb.ToString());
return props.ToList();
}
public static List<ClassPropValue> GetClassPropDictionary(object elem)
{
var type = elem.GetType();
var props = type.GetProperties();
var propValues = new List<ClassPropValue>();
foreach (var prop in props)
{
//如果该属性是索引 跳过本次循环。避免提示异常信息:参数计数不匹配
if (prop.GetIndexParameters().Length > 0)
{
continue;
}
ClassPropValue classPropValue = new ClassPropValue()
{
ClassName = $"Class类型名-{type.FullName}",
//{prop.DeclaringType}区分属性所属的声明类型
PropName = $"{prop.Name}",
PropValue = prop.GetValue(elem),
DataType = prop.PropertyType
};
propValues.Add(classPropValue);
}
return propValues;
}
}
}