当前位置: 首页>>代码示例>>C#>>正文


C# CodeStatementCollection.DeclareVar方法代码示例

本文整理汇总了C#中System.CodeDom.CodeStatementCollection.DeclareVar方法的典型用法代码示例。如果您正苦于以下问题:C# CodeStatementCollection.DeclareVar方法的具体用法?C# CodeStatementCollection.DeclareVar怎么用?C# CodeStatementCollection.DeclareVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.CodeDom.CodeStatementCollection的用法示例。


在下文中一共展示了CodeStatementCollection.DeclareVar方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetInitializeMethodTryStatements

        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)))));

                }
//.........这里部分代码省略.........
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:101,代码来源:ClassGenerator.cs


注:本文中的System.CodeDom.CodeStatementCollection.DeclareVar方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。