예제 #1
0
        /// <summary>添加索引器</summary>
        public CodeMemberProperty AddIndexs()
        {
            var p = new CodeMemberProperty();

            p.Attributes = MemberAttributes.Public | MemberAttributes.Override;
            p.Name       = "Item";
            p.Type       = new CodeTypeReference(typeof(Object));
            p.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "name"));
            p.AddSummary("获取/设置 字段值。");
            p.AddParamComment("name", "属性名");
            p.AddReturnComment("属性值");

            p.HasGet = true;
            p.HasSet = true;

            foreach (var item in Table.Columns)
            {
                var name = item.Name;

                // 取值
                var cond = "$name".Equal(name);
                p.GetStatements.Add(cond.IfTrue(("_" + name).Return()));

                // 设置值

                var            type = typeof(Convert);
                var            mi   = type.GetMethod("To" + item.DataType.Name, new Type[] { typeof(Object) });
                CodeExpression ce   = null;
                if (mi != null)
                {
                    ce = typeof(Convert).ToExp().Invoke("To" + item.DataType.Name, "@value");
                }
                else
                {
                    ce = "@value".Cast(item.DataType);
                }

                p.SetStatements.Add(cond.IfTrue(("_" + name).Assign(ce), new CodeMethodReturnStatement()));
            }
            // 取值
            p.GetStatements.Add("$base[$name]".Return());

            // 设置值
            p.SetStatements.Add("$base[$name]".Assign("$value"));

            p.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "获取/设置 字段值"));
            p.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, null));
            Class.Members.Add(p);

            return(p);
        }
예제 #2
0
        /// <summary>添加索引器</summary>
        public CodeMemberProperty AddIndexs()
        {
            var p = new CodeMemberProperty();

            p.Attributes = MemberAttributes.Public | MemberAttributes.Override;
            p.Name       = "Item";
            p.Type       = new CodeTypeReference(typeof(Object));
            p.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "name"));
            //p.Comments.Add(AddSummary("获取/设置 字段值。"));
            //p.Comments.Add(AddParamComment("name", "属性名"));
            //p.Comments.Add(AddComment("return", "属性值"));
            p.AddSummary("获取/设置 字段值。");
            p.AddParamComment("name", "属性名");
            p.AddReturnComment("属性值");

            p.HasGet = true;
            p.HasSet = true;

            foreach (var item in Table.Columns)
            {
                //String name = FieldNames[item.Name];
                var name = item.Name;

                // 取值
                //var cond = new CodeConditionStatement();
                //p.GetStatements.Add(cond);
                //cond.Condition = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("name"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(name));
                //cond.TrueStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(null, "_" + name)));
                var cond = "$name".Equal(name);
                p.GetStatements.Add(cond.IfTrue(("_" + name).Return()));

                // 设置值
                //cond = new CodeConditionStatement();
                //p.SetStatements.Add(cond);
                //cond.Condition = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("name"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(name));

                var            type = typeof(Convert);
                var            mi   = type.GetMethod("To" + item.DataType.Name, new Type[] { typeof(Object) });
                CodeExpression ce   = null;
                if (mi != null)
                {
                    //var mie = new CodeMethodInvokeExpression();
                    //mie.Method = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Convert)), "To" + item.DataType.Name);
                    //mie.Parameters.Add(new CodeArgumentReferenceExpression("value"));
                    //// _Name = Convert.ToString(value);
                    //ce = mie;

                    ce = typeof(Convert).ToExp().Invoke("To" + item.DataType.Name, "@value");
                }
                else
                {
                    //var cce = new CodeCastExpression();
                    //cce.TargetType = new CodeTypeReference(item.DataType);
                    //cce.Expression = new CodeArgumentReferenceExpression("value");
                    //ce = cce;

                    ce = "@value".Cast(item.DataType);
                }
                //cond.TrueStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(null, "_" + name), ce));

                //// return;
                //cond.TrueStatements.Add(new CodeMethodReturnStatement());

                p.SetStatements.Add(cond.IfTrue(("_" + name).Assign(ce), new CodeMethodReturnStatement()));
            }
            // 取值
            //var cmrs = new CodeMethodReturnStatement();
            //cmrs.Expression = new CodeIndexerExpression(new CodeBaseReferenceExpression(), new CodeVariableReferenceExpression("name"));
            //p.GetStatements.Add(cmrs);
            p.GetStatements.Add("$base[$name]".Return());

            // 设置值
            //var cas = new CodeAssignStatement();
            //cas.Left = new CodeIndexerExpression(new CodeBaseReferenceExpression(), new CodeVariableReferenceExpression("name"));
            //cas.Right = new CodeVariableReferenceExpression("value");
            //p.SetStatements.Add(cas);
            p.SetStatements.Add("$base[$name]".Assign("$value"));

            p.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "获取/设置 字段值"));
            p.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, null));
            Class.Members.Add(p);

            return(p);
        }