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 { /// /// 获取元素的所有属性 /// /// public static List GetECProperties(Element element) { if (null == element) { return null; } var props = new List(); 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 GetClassPropDictionary(object elem) { var type = elem.GetType(); var props = type.GetProperties(); var propValues = new List(); 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; } } }