Ejemplo n.º 1
0
        CodeStatementCollection GetInitializeMethodTryStatements(CodeExpression xmlReaderVar, CodeExpression xamlReaderVar, CodeExpression objWriterVar,
            CodeExpression initializeXamlVar, ClassData classData, List<CodeMemberField> memberFields)
        {
            CodeStatementCollection tryStatements = new CodeStatementCollection();

            // System.Xaml.XamlSchemaContext schemaContext = _XamlStaticHelperNamespace._XamlStaticHelper.SchemaContext;
            CodeVariableReferenceExpression SchemaContextReference = new CodeVariableReferenceExpression(classData.HelperClassFullName + ".SchemaContext");
            CodeVariableReferenceExpression SchemaContext = tryStatements.DeclareVar(typeof(XamlSchemaContext), "schemaContext", SchemaContextReference);

            //    xmlReader = System.Xml.XmlReader.Create(initializeXaml);
            CodeExpression xmlReader = new CodeMethodInvokeExpression(
                new CodeMethodReferenceExpression()
                {
                    MethodName = "System.Xml.XmlReader.Create"
                },
                initializeXamlVar);
            tryStatements.Add(new CodeAssignStatement(xmlReaderVar, xmlReader));

            //   System.Xaml.XamlXmlReaderSettings readerSettings = new System.Xaml.XamlXmlReaderSettings();
            CodeVariableReferenceExpression readerSettingsVar = tryStatements.DeclareVar(
                    typeof(XamlXmlReaderSettings), "readerSettings", typeof(XamlXmlReaderSettings).New());

            //  readerSettings.LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            tryStatements.Add(
                new CodeAssignStatement(
                    new CodePropertyReferenceExpression(readerSettingsVar, "LocalAssembly"),
                    new CodeMethodInvokeExpression(
                       new CodeMethodReferenceExpression()
                       {
                           MethodName = "System.Reflection.Assembly.GetExecutingAssembly"
                       })));

            //  readerSettings.AllowProtectedMembersOnRoot = true;
            tryStatements.Add(
                new CodeAssignStatement(
                    new CodePropertyReferenceExpression(readerSettingsVar, "AllowProtectedMembersOnRoot"),
                    new CodePrimitiveExpression(true)));

            //  reader = new System.Xaml.XamlXmlReader(xmlReader, schemaContext, readerSettings);
            CodeExpression newReader = typeof(XamlXmlReader).New(xmlReaderVar, SchemaContext, readerSettingsVar);
            tryStatements.Add(new CodeAssignStatement(xamlReaderVar, newReader));

            //     XamlObjectWriterSettings writerSettings = new XamlObjectWriterSettings();
            CodeVariableReferenceExpression writerSettingsVar = tryStatements.DeclareVar(
                typeof(XamlObjectWriterSettings), "writerSettings", typeof(XamlObjectWriterSettings).New());

            //  writerSettings.RootObjectInstance = this;
            tryStatements.Add(new CodeAssignStatement()
            {
                Left = writerSettingsVar.Property("RootObjectInstance"),
                Right = CodeThis
            });

            //  writerSettings.AccessLevel = System.Xaml.Permissions.XamlAccessLevel.PrivateAccessTo(typeof(<TypeBeingGenerated>));
            tryStatements.Add(new CodeAssignStatement()
            {
                Left = writerSettingsVar.Property("AccessLevel"),
                Right = new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression()
                            {
                                MethodName = "System.Xaml.Permissions.XamlAccessLevel.PrivateAccessTo"
                            },
                            new CodeTypeOfExpression(classData.Name)
                        )
            });

            //     var writer = new XamlObjectWriter(schemaContext, settings);
            //
            CodeExpression newObjectWriter = typeof(XamlObjectWriter).New(SchemaContext, writerSettingsVar);
            tryStatements.Add(new CodeAssignStatement(objWriterVar, newObjectWriter));

            //      XamlServices.Transform(reader, writer);
            //
            tryStatements.Add(
                    new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression()
                        {
                            MethodName = "System.Xaml.XamlServices.Transform"
                        },
                        xamlReaderVar,
                        objWriterVar
                    ));

            //  For all fields generated, generate the wireup to get the value from xaml. For eg, for a field of type "Bar" and name "Baz":
            //  Baz = ((Tests.Build.Tasks.Xaml.Bar)(objectWriter.RootNameScope.FindName("Baz")));
            if (memberFields != null && memberFields.Count > 0)
            {
                foreach (var field in memberFields)
                {
                    tryStatements.Add(
                        new CodeAssignStatement(
                            new CodeVariableReferenceExpression(field.Name),
                            new CodeCastExpression(
                                field.Type,
                                new CodeMethodInvokeExpression(
                                    new CodePropertyReferenceExpression(objWriterVar, "RootNameScope"),
                                    "FindName",
                                    new CodePrimitiveExpression(field.Name)))));

                }
            }

            return tryStatements;
        }