/// <summary> /// 初始化需要扫描的字段集合 /// </summary> /// <param name="scanner">对象扫描</param> internal void CreateScanFiled(ReflectionTypeScanner scanner) { BaseType = (ReflectionObjectType)scanner.GetObjectType(Type.BaseType); List <KeyValue <FieldInfo, ReflectionType> > fieldList = null; foreach (FieldInfo field in Type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { Type fieldType = field.FieldType; if (!fieldType.IsPointer && fieldType != typeof(System.Reflection.Pointer)) { ReflectionType objectType = scanner.GetObjectType(fieldType); if (objectType.IsScan != 0 || IsScanDerived(fieldType)) { if (fieldList == null) { fieldList = new List <KeyValue <FieldInfo, ReflectionType> >(); } fieldList.Add(new KeyValue <FieldInfo, ReflectionType>(field, objectType)); } } } if (fieldList != null) { ScanFields = fieldList.ToArray(); } else if (BaseType.IsScan == 0) { IsScan = 0; } }
/// <summary> /// 扫描根对象 /// </summary> /// <param name="fieldInfo">根对象静态字段</param> /// <param name="value">根对象</param> protected override void scanRoot(FieldInfo fieldInfo, object value) { Type fieldType = value.GetType(); ReflectionType objectType = GetObjectType(fieldType); if (objectType.IsScan != 0) { if (fieldType.IsClass) { objectType.Add(fieldInfo, this); if (fieldType == typeof(string)) { ((ReflectionArrayType)objectType).ElementCount += ((string)value).Length; } else { new ReflectionScanner(this, fieldInfo).ScanObject(value, objectType); } } else { new ReflectionScanner(this, fieldInfo).Scan(value, objectType); } } }
/// <summary> /// 读取下一个数据 /// </summary> /// <param name="scanner"></param> internal void Next(ref ReflectionScanner scanner) { object value = this.value; bool isArray = this.isArray; int isEndIndex = 0; do { KeyValue <FieldInfo, ReflectionType> field = this.objectType.ScanFields[index++]; if (index == this.objectType.ScanFields.Length) { --scanner.Objects.Length; if (this.objectType.BaseType.IsScan != 0) { this.objectType.BaseType.Append(ref scanner, value, isArray); } isEndIndex = 1; } object fieldValue = field.Key.GetValue(value); if (fieldValue != null) { ReflectionType objectType = field.Value; Type fieldType = fieldValue.GetType(); if (fieldType != objectType.Type) { objectType = scanner.Scanner.GetObjectType(fieldType); } if (objectType.IsScan != 0) { if (fieldType.IsClass) { if (scanner.AddObject(fieldValue)) { if (!isArray) { objectType.Add(scanner.FieldInfo, scanner.Scanner); } if (objectType.Append(ref scanner, fieldValue, false) == 0) { return; } } else if (scanner.IsLimitExceeded) { return; } } else if (objectType.Append(ref scanner, fieldValue, false) == 0) { return; } } } }while (isEndIndex == 0); }
/// <summary> /// 获取数组元素类型 /// </summary> /// <param name="scanner"></param> internal void GetElementType(ReflectionTypeScanner scanner) { Type ElementType = Type.GetElementType(); if (!ElementType.IsPointer && ElementType != typeof(System.Reflection.Pointer)) { this.ElementType = scanner.GetObjectType(ElementType); if (this.ElementType.IsScan != 0 || IsScanDerived(ElementType)) { IsScan = 2; } } else { this.ElementType = ReflectionObjectType.NullType; } }
/// <summary> /// 对象扫描 /// </summary> /// <param name="value"></param> /// <param name="objectType"></param> internal void Scan(object value, ReflectionType objectType) { switch (objectType.Append(ref this, value, false)) { case 0: scan(); return; case 1: do { Arrays.Array[Arrays.Length - 1].Next(ref this); if (Objects.Length != 0) { scan(); return; } }while (Arrays.Length != 0); return; } }
/// <summary> /// 读取下一个数据 /// </summary> /// <param name="scanner"></param> internal void Next(ref ReflectionScanner scanner) { while (enumerator.MoveNext()) { object elementValue = enumerator.Current; if (elementValue != null) { Type fieldType = elementValue.GetType(); if (fieldType != objectType.Type) { objectType = scanner.Scanner.GetObjectType(fieldType); } if (objectType.IsScan != 0) { if (fieldType.IsClass) { if (scanner.AddObject(elementValue)) { if (objectType.Append(ref scanner, elementValue, true) != 2) { return; } } else if (scanner.IsLimitExceeded) { return; } } else if (objectType.Append(ref scanner, elementValue, true) != 2) { return; } } } } --scanner.Arrays.Length; }
/// <summary> /// 数组类型 /// </summary> /// <param name="type">类型</param> internal ReflectionArrayType(Type type) : base(type) { IsScan = 1; ElementType = ReflectionObjectType.NullType; }
/// <summary> /// string /// </summary> internal ReflectionArrayType() : base(typeof(string)) { IsScan = 1; ElementType = ReflectionObjectType.NullType; }
/// <summary> /// 反射模式数组扫描 /// </summary> /// <param name="value">数组对象</param> /// <param name="objectType">对象类型</param> internal ReflectionArrayScanner(object value, ReflectionType objectType) { enumerator = ((Array)value).GetEnumerator(); this.objectType = objectType; }
/// <summary> /// 对象扫描 /// </summary> /// <param name="value"></param> /// <param name="objectType"></param> internal void ScanObject(object value, ReflectionType objectType) { objectReferences.Add(new ObjectReference(value)); Scan(value, objectType); }