public virtual PropertyDeclaration CreateProperty(IField field, bool createGetter, bool createSetter) { ClassFinder targetContext = new ClassFinder(field); string name = GetPropertyName(field.Name); PropertyDeclaration property = new PropertyDeclaration(ConvertModifier(field.Modifiers, targetContext), null, name, null); property.TypeReference = ConvertType(field.ReturnType, new ClassFinder(field)); if (createGetter) { BlockStatement block = new BlockStatement(); block.Return(new IdentifierExpression(field.Name)); property.GetRegion = new PropertyGetRegion(block, null); } if (createSetter) { BlockStatement block = new BlockStatement(); block.Assign(new IdentifierExpression(field.Name), new IdentifierExpression("value")); property.SetRegion = new PropertySetRegion(block, null); } property.Modifier = Modifiers.Public | (property.Modifier & Modifiers.Static); return(property); }
INode InitStaticVariable(string initFieldName, string variableName, Expression initializer, TypeDeclaration typeDeclaration) { const string helperMethodName = "InitStaticVariableHelper"; if (typeDeclaration != null) { if (!typeDeclaration.Children.OfType <MethodDeclaration>().Any(m => m.Name == helperMethodName)) { // add helper method var helperMethod = new MethodDeclaration { Name = helperMethodName, Modifier = Modifiers.Static, TypeReference = new TypeReference("System.Boolean", true), Parameters = { new ParameterDeclarationExpression(new TypeReference("Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag"), "flag") }, Body = new BlockStatement() }; BlockStatement trueBlock = new BlockStatement(); BlockStatement elseIfBlock = new BlockStatement(); BlockStatement falseBlock = new BlockStatement(); helperMethod.Body.AddStatement( new IfElseStatement(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(0))) { TrueStatement = { trueBlock }, ElseIfSections = { new ElseIfSection(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(2)), elseIfBlock) }, FalseStatement = { falseBlock } }); trueBlock.Assign(ExpressionBuilder.Identifier("flag").Member("State"), new PrimitiveExpression(2)); trueBlock.Return(new PrimitiveExpression(true)); elseIfBlock.Throw(new TypeReference("Microsoft.VisualBasic.CompilerServices.IncompleteInitialization").New()); falseBlock.Return(new PrimitiveExpression(false)); typeDeclaration.AddChild(helperMethod); } } BlockStatement tryBlock = new BlockStatement(); BlockStatement ifTrueBlock = new BlockStatement(); tryBlock.AddStatement(new IfElseStatement(ExpressionBuilder.Identifier(helperMethodName).Call(ExpressionBuilder.Identifier(initFieldName)), ifTrueBlock)); ifTrueBlock.Assign(ExpressionBuilder.Identifier(variableName), initializer); BlockStatement finallyBlock = new BlockStatement(); finallyBlock.Assign(ExpressionBuilder.Identifier(initFieldName).Member("State"), new PrimitiveExpression(1)); BlockStatement lockBlock = new BlockStatement(); lockBlock.AddStatement(new TryCatchStatement(tryBlock, null, finallyBlock)); return(new LockStatement(ExpressionBuilder.Identifier(initFieldName), lockBlock)); }