Пример #1
0
        private static void ProvideNestedToStringWithCultureMethod(CodeTypeDeclaration nest, CodeFieldReferenceExpression formatProviderRef)
        {
            CodeParameterDeclarationExpression codeParameterDeclarationExpression = new CodeParameterDeclarationExpression(typeof(object), "objectToConvert");
            CodeVariableReferenceExpression    codeVariableReferenceExpression    = new CodeVariableReferenceExpression("t");
            CodeVariableReferenceExpression    codeVariableReferenceExpression2   = new CodeVariableReferenceExpression("method");
            CodeMemberMethod codeMemberMethod = CodeDomHelpers.CreateMethod(typeof(string), "ToStringWithCulture", "This is called from the compile/run appdomain to convert objects within an expression block to a string", (MemberAttributes)24578, CodeDomHelpers.CheckNullParameter(codeParameterDeclarationExpression.Name), new CodeVariableDeclarationStatement(typeof(Type), codeVariableReferenceExpression.VariableName, codeParameterDeclarationExpression.Ref().Call("GetType")), new CodeVariableDeclarationStatement(typeof(MethodInfo), codeVariableReferenceExpression2.VariableName, codeVariableReferenceExpression.Call("GetMethod", "ToString".Prim(), new CodeArrayCreateExpression(typeof(Type), new CodeExpression[1]
            {
                new CodeTypeOfExpression(typeof(IFormatProvider))
            }))), new CodeConditionStatement(new CodeBinaryOperatorExpression(codeVariableReferenceExpression2, CodeBinaryOperatorType.IdentityEquality, CodeDomHelpers.nullEx), new CodeStatement[1]
            {
                new CodeMethodReturnStatement(codeParameterDeclarationExpression.Ref().Call("ToString"))
            }, new CodeStatement[1]
            {
                new CodeMethodReturnStatement(new CodeCastExpression(typeof(string), codeVariableReferenceExpression2.Call("Invoke", codeParameterDeclarationExpression.Ref(), new CodeArrayCreateExpression(typeof(object), new CodeExpression[1]
                {
                    formatProviderRef
                }))))
            }));

            codeMemberMethod.Parameters.Add(codeParameterDeclarationExpression);
            nest.Members.Add(codeMemberMethod);
        }
Пример #2
0
        private static void ProvideNestedToStringWithCultureMethod(CodeTypeDeclaration nest, CodeFieldReferenceExpression formatProviderRef)
        {
            CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(typeof(object), "objectToConvert");
            CodeVariableReferenceExpression    callSite  = new CodeVariableReferenceExpression("t");
            CodeVariableReferenceExpression    left      = new CodeVariableReferenceExpression("method");
            CodeMemberMethod method = CodeDomHelpers.CreateMethod(typeof(string), "ToStringWithCulture", "This is called from the compile/run appdomain to convert objects within an expression block to a string", MemberAttributes.Public | MemberAttributes.Final, new CodeObject[] { CodeDomHelpers.CheckNullParameter(parameter.Name), new CodeVariableDeclarationStatement(typeof(Type), callSite.VariableName, parameter.Ref().Call("GetType", new CodeExpression[0])), new CodeVariableDeclarationStatement(typeof(MethodInfo), left.VariableName, callSite.Call("GetMethod", new CodeExpression[] { "ToString".Prim(), new CodeArrayCreateExpression(typeof(Type), new CodeExpression[] { new CodeTypeOfExpression(typeof(IFormatProvider)) }) })), new CodeConditionStatement(new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityEquality, CodeDomHelpers.nullEx), new CodeStatement[] { new CodeMethodReturnStatement(parameter.Ref().Call("ToString", new CodeExpression[0])) }, new CodeStatement[] { new CodeMethodReturnStatement(new CodeCastExpression(typeof(string), left.Call("Invoke", new CodeExpression[] { parameter.Ref(), new CodeArrayCreateExpression(typeof(object), new CodeExpression[] { formatProviderRef }) }))) }) });

            method.Parameters.Add(parameter);
            nest.Members.Add(method);
        }
Пример #3
0
        private static void ProvideWriteMethod1(CodeTypeMemberCollection members, CodeMemberField currentIndent, CodeMemberField endsWithNewline, CodeMemberProperty GenerationEnvironment, CodeVariableReferenceExpression textToAppend)
        {
            CodeExpression   codeExpression   = typeof(Environment).Expr().Prop("NewLine");
            CodeMemberMethod codeMemberMethod = CodeDomHelpers.CreateMethod(null, "Write", "Write text directly into the generated output", (MemberAttributes)24578, new CodeConditionStatement(CodeDomHelpers.Call(typeof(string), "IsNullOrEmpty", textToAppend), new CodeMethodReturnStatement()), new CodeCommentStatement("If we're starting off, or if the previous text ended with a newline,"), new CodeCommentStatement("we have to append the current indent first."), new CodeConditionStatement(new CodeBinaryOperatorExpression(GenerationEnvironment.Ref().Prop("Length").VEquals(0.Prim()), CodeBinaryOperatorType.BooleanOr, endsWithNewline.Ref()), GenerationEnvironment.Ref().CallS("Append", currentIndent.Ref()), endsWithNewline.Ref().Assign(false.Prim())), new CodeCommentStatement("Check if the current text ends with a newline"), new CodeConditionStatement(textToAppend.Call("EndsWith", codeExpression, typeof(StringComparison).Expr().Prop("CurrentCulture")), new CodeAssignStatement(endsWithNewline.Ref(), true.Prim())), new CodeCommentStatement("This is an optimization. If the current indent is \"\", then we don't have to do any"), new CodeCommentStatement("of the more complex stuff further down."), new CodeConditionStatement(currentIndent.Ref().Prop("Length").VEquals(0.Prim()), GenerationEnvironment.Ref().CallS("Append", textToAppend), new CodeMethodReturnStatement()), new CodeCommentStatement("Everywhere there is a newline in the text, add an indent after it"), textToAppend.Assign(textToAppend.Call("Replace", codeExpression, codeExpression.Add(currentIndent.Ref()))), new CodeCommentStatement("If the text ends with a newline, then we should strip off the indent added at the very end"), new CodeCommentStatement("because the appropriate indent will be added when the next time Write() is called"), new CodeConditionStatement(endsWithNewline.Ref(), new CodeStatement[1]
            {
                GenerationEnvironment.Ref().CallS("Append", textToAppend, 0.Prim(), textToAppend.Prop("Length").Subtract(currentIndent.Ref().Prop("Length")))
            }, new CodeStatement[1]
            {
                GenerationEnvironment.Ref().CallS("Append", textToAppend)
            }));

            codeMemberMethod.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Transform-time helpers"));
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), textToAppend.VariableName));
            members.Add(codeMemberMethod);
        }