Exemple #1
0
        public void TestAssignIndexer()
        {
            var c = new CodeDomGenerator();

            c.AddNamespace("Samples")
            .AddClass("cls")
            .AddMethod(MemberAttributes.Public | MemberAttributes.Static, () => "foo",
                       Emit.declare("b", () => new int[10]),
                       Emit.assignExp((int[] b) => b[0], CodeDom.GetExpression(() => 1)),
                       Emit.declare("a", () => new List <int>()),
                       Emit.assignExp((List <int> a) => a[0], CodeDom.GetExpression(() => 1))
                       )
            ;

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.CSharp));

            var ass = c.Compile();

            Assert.IsNotNull(ass);

            Type TestClass = ass.GetType("Samples.cls");

            Assert.IsNotNull(TestClass);

            Console.WriteLine(c.GenerateCode(CodeDomGenerator.Language.VB));

            ass = c.Compile();

            Assert.IsNotNull(ass);

            TestClass = ass.GetType("Samples.cls");

            Assert.IsNotNull(TestClass);
        }
Exemple #2
0
        public void TestGetExpression()
        {
            CodeExpression exp = CodeDom.GetExpression(() => 1);

            Assert.IsTrue(exp is CodePrimitiveExpression);
            Assert.AreEqual(1, ((CodePrimitiveExpression)exp).Value);

            CodeExpression exp2 = CodeDom.GetExpression((int g) => 1 + g);

            Assert.IsTrue(exp2 is CodeBinaryOperatorExpression);

            CodeExpression exp3 = CodeDom.GetExpression((int g) => [email protected] <int>("d") + g - 10);

            Assert.IsTrue(exp3 is CodeBinaryOperatorExpression);

            CodeExpression exp4 = CodeDom.GetExpression(() => CodeDom.Call(exp, "ToString")());

            Assert.IsTrue(exp4 is CodeMethodInvokeExpression);
            Assert.AreEqual(exp, ((CodeMethodInvokeExpression)exp4).Method.TargetObject);

            CodeExpression exp5 = CodeDom.GetExpression(() => CodeDom.Call(exp3, "ToString")());

            Assert.IsTrue(exp5 is CodeMethodInvokeExpression);
            Assert.AreEqual(exp3, ((CodeMethodInvokeExpression)exp5).Method.TargetObject);
        }
Exemple #3
0
        public void TestGetExpressionInject()
        {
            CodeExpression exp = CodeDom.GetExpression(() => 1);

            Assert.IsTrue(exp is CodePrimitiveExpression);
            Assert.AreEqual(1, ((CodePrimitiveExpression)exp).Value);

            CodeExpression exp2 = CodeDom.GetExpression((int g) => CodeDom.InjectExp <int>(0) + g, exp);

            Assert.IsTrue(exp2 is CodeBinaryOperatorExpression);
            Assert.AreSame(exp, ((CodeBinaryOperatorExpression)exp2).Left);
            Assert.IsTrue(((CodeBinaryOperatorExpression)exp2).Right is CodeVariableReferenceExpression);
        }
        protected virtual void OnPupulateEntityRelations()
        {
            var relationDescType = new CodeTypeReference(typeof(RelationDescEx));

            foreach (var entityRelation in _entity.GetActiveOne2ManyRelations())
            {
                string accessorName = string.IsNullOrEmpty(entityRelation.AccessorName) ? WXMLCodeDomGeneratorNameHelper.GetMultipleForm(entityRelation.Entity.Name) : entityRelation.AccessorName;

                var staticProperty = new CodeMemberProperty
                {
                    Name       = accessorName + "Relation",
                    HasGet     = true,
                    HasSet     = false,
                    Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static,
                    Type       = relationDescType
                };

                var entityTypeExpression     = Settings.UseTypeInProps ? WXMLCodeDomGeneratorHelper.GetEntityClassTypeReferenceExpression(_settings, entityRelation.Entity, entityRelation.Entity.Namespace != _entity.Namespace || _entity.ScopeNames().Any(item => item == entityRelation.Entity.Name)) : WXMLCodeDomGeneratorHelper.GetEntityNameReferenceExpression(_settings, entityRelation.Entity, entityRelation.Entity.Namespace != _entity.Namespace || _entity.ScopeNames().Any(item => item == entityRelation.Entity.Name));
                var selfEntityTypeExpression = Settings.UseTypeInProps ? WXMLCodeDomGeneratorHelper.GetEntityClassTypeReferenceExpression(_settings, _entity, false) : WXMLCodeDomGeneratorHelper.GetEntityNameReferenceExpression(_settings, _entity, false);

                staticProperty.GetStatements.Add(
                    new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(
                            relationDescType,
                            new CodeObjectCreateExpression(
                                typeof(EntityUnion),
                                selfEntityTypeExpression
                                ),
                            new CodeObjectCreateExpression(
                                typeof(RelationDesc),
                                new CodeObjectCreateExpression(
                                    new CodeTypeReference(typeof(EntityUnion)),
                                    entityTypeExpression
                                    ),
                                WXMLCodeDomGeneratorHelper.GetFieldNameReferenceExpression(_settings, entityRelation.Property, entityRelation.Entity.Namespace != _entity.Namespace || _entity.ScopeNames().Any(item => item == entityRelation.Entity.Name)),
                                new CodePrimitiveExpression(entityRelation.Name ?? "default")
                                )
                            )
                        )
                    );

                Members.Add(staticProperty);

                string cd = new WXMLCodeDomGeneratorNameHelper(_settings).GetEntityClassName(entityRelation.Property.Entity, entityRelation.Entity.Namespace != _entity.Namespace || _entity.ScopeNames().Any(item => item == entityRelation.Entity.Name)) + ".Properties";
                //string dn = new WXMLCodeDomGeneratorNameHelper(_settings).GetEntityClassName(entityRelation.Entity, true) + ".Descriptor";

                //CodeDom.Field<string>(CodeDom.TypeRef(dn), "EntityName")

                CodeExpression exp = CodeDom.GetExpression((EntityUnion hostEntity) =>
                                                           new RelationDescEx(hostEntity, new RelationDesc(
                                                                                  new EntityUnion(CodeDom.InjectExp <string>(0)),
                                                                                  CodeDom.Field <string>(CodeDom.TypeRef_str(cd), entityRelation.Property.PropertyAlias),
                                                                                  entityRelation.Name
                                                                                  )), entityTypeExpression);

                Members.Add(Define.Method(MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static,
                                          typeof(RelationDescEx),
                                          (EntityUnion hostEntity) => "Get" + staticProperty.Name,
                                          Emit.@return(exp)
                                          ));

                Members.Add(Define.Method(MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static,
                                          typeof(RelationDescEx),
                                          (EntityUnion hostEntity, EntityUnion joinEntity) => "Get" + staticProperty.Name,
                                          Emit.@return((EntityUnion hostEntity, EntityUnion joinEntity) =>
                                                       new RelationDescEx(hostEntity, new RelationDesc(
                                                                              joinEntity,
                                                                              CodeDom.Field <string>(CodeDom.TypeRef_str(cd), entityRelation.Property.PropertyAlias),
                                                                              entityRelation.Name
                                                                              ))
                                                       )
                                          ));

                var memberProperty = new CodeMemberProperty
                {
                    Name       = accessorName,
                    HasGet     = true,
                    HasSet     = false,
                    Attributes =
                        MemberAttributes.Public | MemberAttributes.Final,
                    Type = new CodeTypeReference(typeof(RelationCmd))
                };

                if (!string.IsNullOrEmpty(entityRelation.AccessorDescription))
                {
                    WXMLCodeDomGenerator.SetMemberDescription(memberProperty, entityRelation.AccessorDescription);
                }

                memberProperty.GetStatements.Add(
                    new CodeMethodReturnStatement(
                        new CodeMethodInvokeExpression(
                            new CodeThisReferenceExpression(),
                            "GetCmd",
                            new CodePropertyReferenceExpression(
                                new CodePropertyReferenceExpression(
                                    null, //WXMLCodeDomGeneratorHelper.GetEntityClassReferenceExpression(_settings, _entity, false),
                                    staticProperty.Name
                                    ),
                                "Rel"
                                )
                            )
                        )
                    );
                Members.Add(memberProperty);

                _gen.RaisePropertyCreated(null, this, memberProperty, null);
            }
        }