GenerateCodeFromExpression() 공개 메소드

public GenerateCodeFromExpression ( CodeExpression expression, TextWriter writer, CodeGeneratorOptions options ) : void
expression CodeExpression
writer System.IO.TextWriter
options CodeGeneratorOptions
리턴 void
 // courtesy of http://metasharp.net/index.php?title=More_technical_Csharp#Generating_the_.27is.27_operator
 public static string ExpressionToString(CodeExpression expr, CodeDomProvider provider)
 {
     using (TextWriter m = new StringWriter())
     {
         provider.GenerateCodeFromExpression(expr, m, new CodeGeneratorOptions());
         return m.ToString();
     }
 }
예제 #2
0
        private static string BuildCodeExtractingModelType(CodeDomProvider provider)
        {
            var getType = new CodeMethodInvokeExpression
                (
                    new CodeThisReferenceExpression(),
                    "GetType"
                );

            var baseType = new CodeFieldReferenceExpression
                (
                    getType,
                    "BaseType"
                );

            var getFirstGenericArgument = new CodeArrayIndexerExpression
                (
                    new CodeMethodInvokeExpression
                        (
                        baseType,
                        "GetGenericArguments"
                        ),
                    new CodePrimitiveExpression(0)
                );

            var fullName = new CodeFieldReferenceExpression
                (
                    getFirstGenericArgument,
                    "FullName"
                );

            string modelTypeCode;
            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(fullName, writer, new CodeGeneratorOptions());
                modelTypeCode = writer.ToString();
            }
            return modelTypeCode;
        }
예제 #3
0
        private static string GenerateCode(CodeObject c, CodeDomProvider provider)
        {
            var sb = new StringBuilder();
            var writer = new StringWriter(sb);
            var options = new CodeGeneratorOptions();

            if (c is CodeStatement)
            {
                provider.GenerateCodeFromStatement((CodeStatement)c, writer, options);
            }
            else if (c is CodeCompileUnit)
            {
                provider.GenerateCodeFromCompileUnit((CodeCompileUnit)c, writer, options);
            }
            else if (c is CodeExpression)
            {
                provider.GenerateCodeFromExpression((CodeExpression)c, writer, options);
            }
            else if (c is CodeTypeMember)
            {
                provider.GenerateCodeFromMember((CodeTypeMember)c, writer, options);
            }
            else if (c is CodeTypeDeclaration)
            {
                provider.GenerateCodeFromType((CodeTypeDeclaration)c, writer, options);
            }
            else if (c is CodeNamespace)
            {
                provider.GenerateCodeFromNamespace((CodeNamespace)c, writer, options);
            }
            else
            {
                Assert.False(true, $"Unknown type: {c.GetType()}");
            }

            return sb.ToString();
        }
예제 #4
0
        private static string GenerateCode(CodeObject c, CodeDomProvider provider)
        {
            var sb = new StringBuilder();
            var writer = new StringWriter(sb);
            var options = new CodeGeneratorOptions();

            if (c is CodeStatement)
            {
                provider.GenerateCodeFromStatement((CodeStatement)c, writer, options);
            }
            else if (c is CodeCompileUnit)
            {
                provider.GenerateCodeFromCompileUnit((CodeCompileUnit)c, writer, options);
            }
            else if (c is CodeExpression)
            {
                provider.GenerateCodeFromExpression((CodeExpression)c, writer, options);
            }
            else if (c is CodeTypeMember)
            {
                provider.GenerateCodeFromMember((CodeTypeMember)c, writer, options);
            }
            else if (c is CodeTypeDeclaration)
            {
                provider.GenerateCodeFromType((CodeTypeDeclaration)c, writer, options);
            }
            else if (c is CodeNamespace)
            {
                provider.GenerateCodeFromNamespace((CodeNamespace)c, writer, options);
            }
            else
            {
                throw new ArgumentException($"Tests not set up for unexpected type: {c.GetType()}");
            }

            return sb.ToString();
        }
            public void GenerateDeclaration(TextWriter tw, CodeDomProvider provider, CodeGeneratorOptions opts, CodeTypeReference returnType, CodeParameterDeclarationExpression[] parameters)
            {
                tw.Write(string.Format("Public Shared {0} Operator CType", OperatorName));
                tw.Write("(");
                for (int i = 0; i < parameters.Length; i++)
                {
                    var parameter = parameters[i];
                    provider.GenerateCodeFromExpression(parameter, tw, opts);
                    if (i != (parameters.Length - 1))
                        tw.Write(", ");
                }

                tw.Write(") As ");
                provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(returnType), tw, opts);
                tw.WriteLine();
            }