本文整理汇总了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)))));
}
//.........这里部分代码省略.........