Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        /// <summary>添加单个属性</summary>
        /// <param name="field">字段</param>
        public CodeMemberProperty AddProperty(IDataColumn field)
        {
            //String name = FieldNames[field.Name];
            var name = field.Name;

            var p = new CodeMemberProperty();

            p.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            p.Name       = name;
            p.Type       = new CodeTypeReference(field.DataType);
            p.AddSummary(field.Description);

            // 特性
            if (!field.Description.IsNullOrWhiteSpace())
            {
                p.AddAttribute <DescriptionAttribute>(field.Description);
            }
            if (!field.DisplayName.IsNullOrWhiteSpace() && field.DisplayName != field.Name)
            {
                p.AddAttribute <DisplayNameAttribute>(field.DisplayName);
            }

            p.AddAttribute <DataObjectFieldAttribute>(field.PrimaryKey, field.Identity, field.Nullable, field.Length);
            p.AddAttribute <BindColumnAttribute>(field.ID, field.ColumnName, field.Description, field.Default == null ? null : field.Default, field.RawType, field.Precision, field.Scale, field.IsUnicode);

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

            var f = ("_" + p.Name).ToExp();

            p.GetStatements.Add(f.Return());

            var changing = "OnPropertyChanging".Invoke(p.Name, "$value");
            var changed  = "OnPropertyChanged".Invoke(p.Name);

            p.SetStatements.Add(changing.IfTrue(f.Assign("$value"), changed.ToStat()));

            Class.Members.Add(p);
            return(p);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        /// <summary>添加单个属性</summary>
        /// <param name="field">字段</param>
        public CodeMemberProperty AddProperty(IDataColumn field)
        {
            //String name = FieldNames[field.Name];
            var name = field.Name;

            var p = new CodeMemberProperty();

            p.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            p.Name       = name;
            p.Type       = new CodeTypeReference(field.DataType);
            //p.Comments.Add(AddSummary(field.Description));
            p.AddSummary(field.Description);

            // 特性
            //if (!field.Description.IsNullOrWhiteSpace())
            //{
            //    p.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DescriptionAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(field.Description))));
            //    p.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DisplayNameAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(field.Description))));
            //}
            if (!field.Description.IsNullOrWhiteSpace())
            {
                p.AddAttribute <DescriptionAttribute>(field.Description);
            }
            if (!field.DisplayName.IsNullOrWhiteSpace() && field.DisplayName != field.Name)
            {
                p.AddAttribute <DisplayNameAttribute>(field.DisplayName);
            }

            //p.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DataObjectFieldAttribute)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.PrimaryKey)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Identity)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Nullable)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Length))
            //   ));
            //p.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(BindColumnAttribute)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.ID)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.ColumnName)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Description)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Default == null ? null : field.Default)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.RawType)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Precision)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.Scale)),
            //    new CodeAttributeArgument(new CodePrimitiveExpression(field.IsUnicode))
            //    ));
            p.AddAttribute <DataObjectFieldAttribute>(field.PrimaryKey, field.Identity, field.Nullable, field.Length);
            p.AddAttribute <BindColumnAttribute>(field.ID, field.ColumnName, field.Description, field.Default == null ? null : field.Default, field.RawType, field.Precision, field.Scale, field.IsUnicode);

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

            var f = ("_" + p.Name).ToExp();

            //p.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(null, "_" + p.Name)));
            p.GetStatements.Add(f.Return());

            //var changing = new CodeMethodInvokeExpression(null, "OnPropertyChanging", new CodePrimitiveExpression(p.Name), new CodeVariableReferenceExpression("value"));
            //var changed = new CodeMethodInvokeExpression(null, "OnPropertyChanged", new CodePrimitiveExpression(p.Name));

            var changing = "OnPropertyChanging".Invoke(p.Name, "$value");
            var changed  = "OnPropertyChanged".Invoke(p.Name);

            //var cas = new CodeAssignStatement();
            //cas.Left = new CodeFieldReferenceExpression(null, "_" + p.Name);
            //cas.Right = new CodeVariableReferenceExpression("value");
            //var cas = f.Assign("$value".ToExp());

            //p.SetStatements.Add(new CodeConditionStatement(changing, cas, changed.ToStat()));
            p.SetStatements.Add(changing.IfTrue(f.Assign("$value"), changed.ToStat()));

            Class.Members.Add(p);
            return(p);
        }