public override void MemberArrayOnceTypeHandler(BuilderInfo info) { MethodHandler.Using(typeof(HashSet <>)); Script.Append( $@"if({NewInstance}.{info.MemberName}!={OldInstance}.{info.MemberName}){{ if({NewInstance}.{info.MemberName}==null || {OldInstance}.{info.MemberName}==null){{ result.Add(""{info.MemberName}"",new DiffModel(){{ Name=""{info.MemberName}"",Value={OldInstance}.{info.MemberName}}}); }}else{{ HashSet<{info.TypeName}> compareNew = new HashSet<{info.TypeName}>({NewInstance}.{info.MemberName}); HashSet<{info.TypeName}> compareOld = new HashSet<{info.TypeName}>({OldInstance}.{info.MemberName}); compareOld.ExceptWith(compareNew); result.Add(""{info.MemberName}"",new DiffModel(){{ Name=""{info.MemberName}"",Value=compareOld}}); }} }} "); }
/// <summary> /// 数组分配方法,数组类型会被拆分,例如:class[]或者int[] /// </summary> /// <param name="type"></param> public void ArrayRouter(Type type) { Type eleType = type.GetElementType(); BuilderInfo typeInfo = new BuilderInfo(); typeInfo.RealType = type; typeInfo.Type = eleType; typeInfo.TypeName = NameReverser.GetName(eleType); typeInfo.AvailableName = AvailableNameReverser.GetName(type); if (IsOnceType(eleType)) { //普通类型处理 ArrayOnceTypeHandler(typeInfo); } else { //复杂类型交由入口处理 EntityHandler(eleType); ArrayEntityHandler(typeInfo); } }
public override void ArrayEntityHandler(BuilderInfo info) { StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.Append($@"Dictionary<string,DiffModel> result = new Dictionary<string,DiffModel>();"); scriptBuilder.Append( $@"if({NewInstance}!={OldInstance}){{ if({NewInstance}==null || {OldInstance}==null){{ result.Add(""different"",new DiffModel(){{ Name=""different"",Value={OldInstance}}}); }}else{{ HashSet<{info.TypeName}> compareOld = new HashSet<{info.TypeName}>({OldInstance}); for(int j = 0; j < OldInstance.Length;j+=1){{ for(int i = 0; i < NewInstance.Length;i+=1){{ if(SnapshotOperator.Diff({NewInstance}[i],{OldInstance}[j]).Count==0){{ compareOld.Remove({OldInstance}[j]); }} }} }} result.Add(""different"",new DiffModel(){{ Name=""different"",Value=compareOld}}); }} }} "); //创建委托 var tempBuilder = FastMethodOperator.New; tempBuilder.ComplierInstance.UseFileComplie(); tempBuilder.Using(info.Type).Using(info.RealType).Using(typeof(HashSet <>));; SnapshotCache[info.Type] = tempBuilder .Using("Natasha") .ClassName("NatashaSnapshot" + info.AvailableName) .MethodName("Compare") .Param(info.Type, OldInstance) .Param(info.Type, NewInstance) .MethodBody(scriptBuilder.ToString()) .Return <Dictionary <string, DiffModel> >() .Complie(); }
public override void MemberArrayEntityHandler(BuilderInfo info) { MethodHandler.Using(info.Type); MethodHandler.Using(info.RealType); MethodHandler.Using(typeof(HashSet <>)); Script.Append( $@"if({NewInstance}.{info.MemberName}!={OldInstance}.{info.MemberName}){{ if({NewInstance}.{info.MemberName}==null || {OldInstance}.{info.MemberName}==null){{ result.Add(""{info.MemberName}"",new DiffModel(){{ Name=""{info.MemberName}"",Value={OldInstance}.{info.MemberName}}}); }}else{{ HashSet<{info.TypeName}> compareOld = new HashSet<{info.TypeName}>({OldInstance}.{info.MemberName}); for(int j = 0; j < {OldInstance}.{info.MemberName}.Length;j+=1){{ for(int i = 0; i < {NewInstance}.{info.MemberName}.Length;i+=1){{ if(SnapshotOperator.Diff({NewInstance}.{info.MemberName}[i],{OldInstance}.{info.MemberName}[j]).Count==0){{ compareOld.Remove({OldInstance}.{info.MemberName}[j]); }} }} }} result.Add(""{info.MemberName}"",new DiffModel(){{ Name=""{info.MemberName}"",Value=compareOld}}); }} }} "); }
public virtual void FieldEntityHandler(BuilderInfo info) { MemberEntityHandler(info); }
/// <summary> /// 复杂类型,比如类、接口、集合类型等 /// </summary> /// <param name="info"></param> public virtual void ArrayEntityHandler(BuilderInfo info) { }
/// <summary> /// 字典实体类型,Dictionary<TKey,TValue> /// </summary> /// <param name="info"></param> public virtual void DictionaryHandler(BuilderInfo info) { }
/// <summary> /// 复杂类型中,复杂类型的成员操作, /// 例如public ClassA SubClass; /// </summary> /// <param name="info"></param> public virtual void MemberEntityHandler(BuilderInfo info) { }
public virtual void PropertyDictionaryHandler(BuilderInfo info) { MemberDictionaryHandler(info); }
public virtual void PropertyCollectionHandler(BuilderInfo info) { MemberCollectionHandler(info); }
public override void EntityReturnHandler(BuilderInfo info) { Script.Append($@"return result;"); }
public override void EntityStartHandler(BuilderInfo info) { Script.Append($@"Dictionary<string,DiffModel> result = new Dictionary<string,DiffModel>();"); }
public override void MemberOnceTypeHandler(BuilderInfo info) { Script.Append($@"if({NewInstance}.{info.MemberName}!={OldInstance}.{info.MemberName}){{ result.Add(""{info.MemberName}"",new DiffModel(){{ Name=""{info.MemberName}"",Value={OldInstance}.{info.MemberName} }}); }}"); }
public virtual void FieldDictionaryHandler(BuilderInfo info) { MemberDictionaryHandler(info); }
public virtual void SelfTypeHandler(BuilderInfo info) { }
public virtual void PropertyArrayOnceTypeHandler(BuilderInfo info) { MemberArrayOnceTypeHandler(info); }
/// <summary> /// 复杂类型刚开始时需要处理的,比如初始化 /// </summary> /// <param name="info"></param> public virtual void EntityStartHandler(BuilderInfo info) { }
public virtual void PropertyEntityHandler(BuilderInfo info) { MemberEntityHandler(info); }
/// <summary> /// 复杂类型结束时需要处理的,比如返回一个实例 /// </summary> /// <param name="info"></param> public virtual void EntityReturnHandler(BuilderInfo info) { }
/// <summary> /// 复杂类型中,一次性数组类型的成员操作, /// 例如public int[] Scores;public string[] Names{get;set;} /// </summary> /// <param name="info"></param> public virtual void MemberArrayOnceTypeHandler(BuilderInfo info) { }
public virtual void FieldArrayOnceTypeHandler(BuilderInfo info) { MemberArrayOnceTypeHandler(info); }
/// <summary> /// 集合实体类型,比如List<T> /// </summary> /// <param name="info"></param> public virtual void CollectionHandler(BuilderInfo info) { }
public virtual void FieldCollectionHandler(BuilderInfo info) { MemberCollectionHandler(info); }
/// <summary> /// 一次性类型处理,比如int,string,枚举这类的 /// </summary> /// <param name="info"></param> public virtual void OnceTypeHandler(BuilderInfo info) { }
/// <summary> /// 实体分配方法,会遍历实体类中的所有成员,不包括方法 /// </summary> /// <param name="type"></param> public void EntityRouter(Type type) { BuilderInfo typeInfo = new BuilderInfo(); typeInfo.RealType = type; typeInfo.TypeName = NameReverser.GetName(type); typeInfo.AvailableName = AvailableNameReverser.GetName(type); EntityStartHandler(typeInfo); //字段克隆 var fields = type.GetFields(); for (int i = 0; i < fields.Length; i++) { var fieldInfo = fields[i]; //排除不能操作的类型 if (!fieldInfo.IsStatic && !fieldInfo.IsInitOnly) { Type fieldType = fieldInfo.FieldType; if (IsOnceType(fieldType)) //普通字段 { BuilderInfo info = new BuilderInfo(); info.MemberName = fieldInfo.Name; info.Type = fieldType; info.TypeName = NameReverser.GetName(fieldType); info.AvailableName = AvailableNameReverser.GetName(fieldType); FieldOnceTypeHandler(info); } else if (fieldType.IsArray) //数组 { Type eleType = fieldType.GetElementType(); BuilderInfo info = new BuilderInfo(); info.MemberName = fieldInfo.Name; info.RealType = eleType; info.Type = eleType; info.TypeName = NameReverser.GetName(eleType); info.AvailableName = AvailableNameReverser.GetName(fieldType); if (IsOnceType(eleType)) { FieldArrayOnceTypeHandler(info); } else { EntityHandler(eleType); FieldArrayEntityHandler(info); } } else if (!fieldType.IsNotPublic) { //检测集合 BuilderInfo info = new BuilderInfo(); info.MemberName = fieldInfo.Name; info.Type = fieldType; info.TypeName = NameReverser.GetName(fieldType); info.AvailableName = AvailableNameReverser.GetName(fieldType); EntityHandler(fieldType); if (fieldType.GetInterface("IEnumerable") != null) { if (fieldType.GetInterface("IDictionary") != null) { if (fieldType.IsInterface) { FieldIDictionaryHandler(info); } else { FieldDictionaryHandler(info); } } else { if (fieldType.IsInterface) { FieldICollectionHandler(info); } else { FieldCollectionHandler(info); } } } else { FieldEntityHandler(info); } } } } //属性克隆 var properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { var propertyInfo = properties[i]; //排除不能操作的属性 if (propertyInfo.CanRead && !propertyInfo.GetGetMethod(true).IsStatic) { Type propertyType = propertyInfo.PropertyType; if (IsOnceType(propertyType)) //普通属性 { BuilderInfo info = new BuilderInfo(); info.MemberName = propertyInfo.Name; info.Type = propertyType; info.TypeName = NameReverser.GetName(propertyType); info.AvailableName = AvailableNameReverser.GetName(propertyType); PropertyOnceTypeHandler(info); } else if (propertyType.IsArray) //数组 { Type eleType = propertyType.GetElementType(); BuilderInfo info = new BuilderInfo(); info.MemberName = propertyInfo.Name; info.RealType = propertyType; info.Type = eleType; info.TypeName = NameReverser.GetName(eleType); info.AvailableName = AvailableNameReverser.GetName(propertyType); if (IsOnceType(eleType)) { PropertyArrayOnceTypeHandler(info); } else { EntityHandler(eleType); PropertyArrayEntityHandler(info); } } else if (!propertyType.IsNotPublic) { //检测集合 BuilderInfo info = new BuilderInfo(); info.MemberName = propertyInfo.Name; info.Type = propertyType; info.TypeName = NameReverser.GetName(propertyType); info.AvailableName = AvailableNameReverser.GetName(propertyType); EntityHandler(propertyType); if (propertyType.GetInterface("IEnumerable") != null) { if (propertyType.GetInterface("IDictionary") != null) { if (propertyType.IsInterface) { PropertyIDictionaryHandler(info); } else { PropertyDictionaryHandler(info); } } else { if (propertyType.IsInterface) { PropertyICollectionHandler(info); } else { PropertyCollectionHandler(info); } } } else { PropertyEntityHandler(info); } } } } EntityReturnHandler(typeInfo); }