示例#1
0
        public Delegate TypeRouter(Model.BuilderInfo info)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($@"{info.DeclaringType.GetDevelopName()} newlist = new {info.DeclaringType.GetDevelopName()}(oldlist.Count);");
            if (info.DeclaringType.IsGenericType)
            {
                sb.AppendLine($@"foreach ({info.DeclaringType.GetGenericArguments()[0].GetDevelopName()} li in oldlist){{");
                // 值类型
                if (info.ArrayBaseType.IsOnceType())
                {
                    sb.Append("newlist.Add(li);");
                }
                else//类
                {
                    sb.Append("var tempinstance = CloneOperator.Clone(li);");
                    sb.Append("newlist.Add(tempinstance);");
                }
            }
            else
            {
                throw new Exception("暂不支持");
            }
            sb.AppendLine(@"}");
            sb.AppendLine(@"return newlist;");

            var action = FastMethodOperator.New
                         .Using("DeepClone")
                         .Using("Natasha")
                         .Using(typeof(Array))
                         .Using(typeof(IList))
                         .OopName($"DeepClone{Guid.NewGuid().ToString().Replace("-", string.Empty)}")
                         .MethodName("Clone")
                         .Param(info.DeclaringType, "oldlist")
                         .MethodBody(sb.ToString())
                         .Return(info.DeclaringType)
                         .Complie();

            return(action);
        }
示例#2
0
        public Delegate TypeRouter(Model.BuilderInfo info)
        {
            var sb = new StringBuilder();

            if (info.ArrayBaseType.IsOnceType())
            {
                sb.AppendLine($@"
                        var newIns = new {info.ElementTypeName}[oldIns.Length];
                        Array.Copy(oldIns, newIns, newIns.Length);
                        return newIns;");
            }
            else if (info.ArrayDimensions > 1) // 多维数组
            {
                // 循环变量数组
                var varNameArr = new string[info.ArrayDimensions];
                // 多维数组长度数组
                var multiArrLenArr = new string[info.ArrayDimensions];

                for (int i = 0; i < info.ArrayDimensions; i++)
                {
                    var varName = $"_{i}";
                    varNameArr[i]     = varName;
                    multiArrLenArr[i] = $"oldIns.GetLength({i})";

                    sb.AppendLine($"for (int {varName} = 0; {varName} < oldIns.GetLength({i}); {varName}++)");
                }
                var varNameStr = string.Join(",", varNameArr);
                sb.AppendLine($@"newIns[{varNameStr}] = CloneOperator.Clone(oldIns[{varNameStr}]);return newIns;");

                var multiArrTypeStr = string.Join(",", multiArrLenArr);
                sb.Insert(0, $"{info.DeclaringType} newIns = new {info.ElementTypeName}[{multiArrTypeStr}];");
            }
            else // 1维数组 || 多维锯齿数组
            {
                sb.AppendLine($@"
{info.DeclaringTypeName} newIns = 
    ({info.DeclaringTypeName})Array.CreateInstance(
    typeof({info.ElementTypeName})
    , oldIns.Length
);
for (int i = 0; i < newIns.Length; i++)
    newIns[i] = CloneOperator.Clone(oldIns[i]);
return newIns;
");
            }

            var tempBuilder = FastMethodOperator.New;

            var action = tempBuilder
                         .Using("DeepClone")
                         .Using("Natasha")
                         .Using(typeof(Array))
                         .OopName($"DeepClone{Guid.NewGuid().ToString().Replace("-", string.Empty)}")
                         .MethodName("Clone")
                         .Param(info.DeclaringType, "oldIns")
                         .MethodBody(sb.ToString())
                         .Return(info.DeclaringType)
                         .Complie();

            return(action);
        }
示例#3
0
        public Delegate TypeRouter(Model.BuilderInfo info)
        {
            StringBuilder scriptBuilder = new StringBuilder();

            scriptBuilder.AppendLine(@"if(old!=default){ return ");


            //初始化目标,区分实体类和接口
            if (!info.DeclaringType.IsInterface)
            {
                scriptBuilder.AppendLine($"new {info.DeclaringTypeName}");
            }
            scriptBuilder.AppendLine(" (old.Select(item=>{{return  new KeyValuePair(");


            //克隆Key
            var keyType = info.DeclaringType.GetGenericArguments()[0];

            if (keyType.IsValueType ||
                keyType == typeof(string) ||
                keyType == typeof(Delegate) ||
                keyType == typeof(MulticastDelegate))
            {
                scriptBuilder.Append($"item.Key,");
            }
            else
            {
                scriptBuilder.AppendLine($"CloneOperator.Clone(item.Key),");
            }


            //克隆Value
            var valueType = info.DeclaringType.GetGenericArguments()[1];

            if (valueType.IsValueType ||
                valueType == typeof(string) ||
                valueType == typeof(Delegate) ||
                valueType == typeof(MulticastDelegate))
            {
                scriptBuilder.Append($"item.Value");
            }
            else
            {
                scriptBuilder.AppendLine($"CloneOperator.Clone(item.Value)");
            }


            //补全括号,返回默认值。
            scriptBuilder.AppendLine(");}));}return default;");


            var action = FastMethodOperator.New
                         .Using("DeepClone")
                         .Using("System.Linq")
                         .Using(typeof(IDictionary))
                         .Using(typeof(KeyValuePair <,>))
                         .Param(info.DeclaringType, "old")
                         .MethodBody(scriptBuilder.ToString())
                         .Return(info.DeclaringType)
                         .Complie();

            return(action);
        }