/// <summary> /// 字典分配方法,将字典拆分处理,例如,IDictionary<TKey,TValue>或Dictionary<TKey,TValue> /// </summary> /// <param name="type"></param> public void DictionaryRouter(Type type) { BuilderInfo typeInfo = new BuilderInfo(); typeInfo.Type = type; typeInfo.TypeName = NameReverser.GetName(type); typeInfo.AvailableName = AvailableNameReverser.GetName(type); if (!type.IsGenericType) { Type[] args = type.GetGenericArguments(); EntityHandler(args[0]); EntityHandler(args[1]); } if (type.IsInterface) { IDictionaryHandler(typeInfo); } else { DictionaryHandler(typeInfo); } }
/// <summary> /// 一次性类型分配方法 /// </summary> /// <param name="type"></param> public void OnceTypeRouter(Type type) { BuilderInfo typeInfo = new BuilderInfo(); typeInfo.Type = type; typeInfo.TypeName = NameReverser.GetName(type); typeInfo.AvailableName = AvailableNameReverser.GetName(type); OnceTypeHandler(typeInfo); }
public Delegate Create() { TypeHandler(CurrentType); //创建委托 MethodHandler.ComplierInstance.UseFileComplie(); var @delegate = MethodHandler .ClassName("NatashaSnapshot" + AvailableNameReverser.GetName(CurrentType)) .MethodName("Compare") .Param(CurrentType, NewInstance) .Param(CurrentType, OldInstance) .MethodBody(Script.ToString()) //方法体 .Return <Dictionary <string, DiffModel> >() //返回类型 .Complie(); return(SnapshotCache[CurrentType] = @delegate); }
/// <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 static string GetAvailableName(this Type type) { return(AvailableNameReverser.GetName(type)); }
/// <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); }