Пример #1
0
 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}});
             }}
         }}   
     ");
 }
Пример #2
0
        /// <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);
            }
        }
Пример #3
0
        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();
        }
Пример #4
0
 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}});
             }}
         }}   
     ");
 }
Пример #5
0
 public virtual void FieldEntityHandler(BuilderInfo info)
 {
     MemberEntityHandler(info);
 }
Пример #6
0
 /// <summary>
 /// 复杂类型,比如类、接口、集合类型等
 /// </summary>
 /// <param name="info"></param>
 public virtual void ArrayEntityHandler(BuilderInfo info)
 {
 }
Пример #7
0
 /// <summary>
 /// 字典实体类型,Dictionary<TKey,TValue>
 /// </summary>
 /// <param name="info"></param>
 public virtual void DictionaryHandler(BuilderInfo info)
 {
 }
Пример #8
0
 /// <summary>
 /// 复杂类型中,复杂类型的成员操作,
 /// 例如public ClassA SubClass;
 /// </summary>
 /// <param name="info"></param>
 public virtual void MemberEntityHandler(BuilderInfo info)
 {
 }
Пример #9
0
 public virtual void PropertyDictionaryHandler(BuilderInfo info)
 {
     MemberDictionaryHandler(info);
 }
Пример #10
0
 public virtual void PropertyCollectionHandler(BuilderInfo info)
 {
     MemberCollectionHandler(info);
 }
Пример #11
0
 public override void EntityReturnHandler(BuilderInfo info)
 {
     Script.Append($@"return result;");
 }
Пример #12
0
 public override void EntityStartHandler(BuilderInfo info)
 {
     Script.Append($@"Dictionary<string,DiffModel> result = new Dictionary<string,DiffModel>();");
 }
Пример #13
0
 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} }});
     }}");
 }
Пример #14
0
 public virtual void FieldDictionaryHandler(BuilderInfo info)
 {
     MemberDictionaryHandler(info);
 }
Пример #15
0
 public virtual void SelfTypeHandler(BuilderInfo info)
 {
 }
Пример #16
0
 public virtual void PropertyArrayOnceTypeHandler(BuilderInfo info)
 {
     MemberArrayOnceTypeHandler(info);
 }
Пример #17
0
 /// <summary>
 /// 复杂类型刚开始时需要处理的,比如初始化
 /// </summary>
 /// <param name="info"></param>
 public virtual void EntityStartHandler(BuilderInfo info)
 {
 }
Пример #18
0
 public virtual void PropertyEntityHandler(BuilderInfo info)
 {
     MemberEntityHandler(info);
 }
Пример #19
0
 /// <summary>
 /// 复杂类型结束时需要处理的,比如返回一个实例
 /// </summary>
 /// <param name="info"></param>
 public virtual void EntityReturnHandler(BuilderInfo info)
 {
 }
Пример #20
0
 /// <summary>
 /// 复杂类型中,一次性数组类型的成员操作,
 /// 例如public int[] Scores;public string[] Names{get;set;}
 /// </summary>
 /// <param name="info"></param>
 public virtual void MemberArrayOnceTypeHandler(BuilderInfo info)
 {
 }
Пример #21
0
 public virtual void FieldArrayOnceTypeHandler(BuilderInfo info)
 {
     MemberArrayOnceTypeHandler(info);
 }
Пример #22
0
 /// <summary>
 /// 集合实体类型,比如List<T>
 /// </summary>
 /// <param name="info"></param>
 public virtual void CollectionHandler(BuilderInfo info)
 {
 }
Пример #23
0
 public virtual void FieldCollectionHandler(BuilderInfo info)
 {
     MemberCollectionHandler(info);
 }
Пример #24
0
 /// <summary>
 /// 一次性类型处理,比如int,string,枚举这类的
 /// </summary>
 /// <param name="info"></param>
 public virtual void OnceTypeHandler(BuilderInfo info)
 {
 }
Пример #25
0
        /// <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);
        }