Пример #1
0
        /// <summary>
        /// Returns a valid C# representation of a typed argument that can be
        /// used in a custom attribute for code generation.
        /// </summary>
        public string ToCSharpString(CustomAttributeTypedArgument argument)
        {
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
                using (var writer = new System.IO.StringWriter(CultureInfo.InvariantCulture))
                {
                    if (argument.Value == null)
                    {
                        provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(null), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                        return(writer.ToString());
                    }

                    if (argument.ArgumentType == typeof(string))
                    {
                        return("\"" + argument.Value + "\"");
                    }

                    if (argument.ArgumentType.IsEnum)
                    {
                        return(argument.ArgumentType.Name + "." + Enum.GetName(argument.ArgumentType, argument.Value));
                    }

                    if (argument.ArgumentType == typeof(Type))
                    {
                        provider.GenerateCodeFromExpression(new System.CodeDom.CodeTypeOfExpression(((Type)argument.Value).Name), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                    }
                    else
                    {
                        provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(argument.Value), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                    }

                    return(writer.ToString());
                }
        }
Пример #2
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("new ");
                        provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference.ArrayElementType), tw, opts);
                        tw.Write("[");
                        for (int i = 0; i < Dimensions.Count; i++)
                        {
                            if (i > 0)
                            {
                                tw.Write(",");
                            }
                            provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                        }
                        tw.Write("]");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("(");
                        for (int i = 0; i < Dimensions.Count; i++)
                        {
                            if (i > 0)
                            {
                                tw.Write(",");
                            }
                            tw.Write("(");
                            provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                            tw.Write(")-1");
                        }
                        tw.Write(") as ");
                        provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference), tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("new ");
                     provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference.ArrayElementType), tw, opts);
                     tw.Write("[");
                     for (int i = 0; i < Dimensions.Count; i++)
                     {
                         if (i > 0)
                             tw.Write(",");
                         provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                     }
                     tw.Write("]");
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("(");
                     for (int i = 0; i < Dimensions.Count; i++)
                     {
                         if (i > 0)
                             tw.Write(",");
                         tw.Write("(");
                         provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                         tw.Write(")-1");
                     }
                     tw.Write(") as ");
                     provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference), tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
Пример #4
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        if (!_eq)
                        {
                            tw.Write("!");
                        }
                        tw.Write("ReferenceEquals(");
                        provider.GenerateCodeFromExpression(_left, tw, opts);
                        tw.Write(", ");
                        provider.GenerateCodeFromExpression(_right, tw, opts);
                        tw.Write(")");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(_left, tw, opts);
                        if (_eq)
                        {
                            tw.Write(" Is ");
                        }
                        else
                        {
                            tw.Write(" IsNot ");
                        }
                        provider.GenerateCodeFromExpression(_right, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #5
0
 protected override void RefreshValue()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
         using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
         {
             tw.Write("(");
             provider.GenerateCodeFromExpression(Expression, tw, opts);
             tw.Write(" is ");
             provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference), tw, opts);
             tw.Write(")");
             Value = tw.InnerWriter.ToString();
         }
     }
 }
Пример #6
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("var {0} = ", provider.CreateEscapedIdentifier(VariableName));
                        provider.GenerateCodeFromExpression(InitExpression, tw, opts);
                        tw.Write(";");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("Dim {0} = ", provider.CreateEscapedIdentifier(VariableName));
                        provider.GenerateCodeFromExpression(InitExpression, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #7
0
        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter         sw   = new StringWriter();
                provider.GenerateCodeFromMember(_property, sw, opts);
                StringReader sr   = new StringReader(sw.GetStringBuilder().ToString());
                string       line = sr.ReadLine();
                while (string.IsNullOrEmpty(line))
                {
                    line = sr.ReadLine();
                }
                int idx = line.IndexOf(" " + _property.Name);

                StringBuilder sb = new StringBuilder();
                sb.Append(" this[");
                foreach (CodeParameterDeclarationExpression parameter in Parameters)
                {
                    provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts);
                    sb.Append(", ");
                }
                sb.Length -= 2;
                sb.Append("]");
                Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx + _property.Name.Length + 1)).ToString();
            }
        }
Пример #8
0
 protected override void RefreshValue()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
         using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
         {
             tw.Write("using (");
             if (UsingExpression != null)
             {
                 provider.GenerateCodeFromExpression(UsingExpression, tw, opts);
             }
             tw.WriteLine(")");
             tw.WriteLine("{");
             tw.Indent++;
             if (Statements != null)
             {
                 foreach (CodeStatement statement in Statements)
                 {
                     provider.GenerateCodeFromStatement(statement, tw, opts);
                 }
             }
             tw.Indent--;
             tw.Write("}");
             Value = tw.InnerWriter.ToString();
         }
     }
 }
Пример #9
0
 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("!");
                     provider.GenerateCodeFromExpression(Expression, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("Not ");
                     provider.GenerateCodeFromExpression(Expression, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
Пример #10
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            if (_left == null)
            {
                return;
            }

            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(_left, tw, opts);
                        if (_right != null)
                        {
                            tw.Write(_separator);
                            provider.GenerateCodeFromExpression(_right, tw, opts);
                        }
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(_left, tw, opts);
                        if (_right != null)
                        {
                            tw.Write(_separator);
                            provider.GenerateCodeFromExpression(_right, tw, opts);
                        }
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #11
0
        static string ToLiteral(string input)
        {
            var writer = new System.IO.StringWriter();

            Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
            provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(input), writer, null);
            return(writer.GetStringBuilder().ToString());
        }
Пример #12
0
 public static string GetCodeCharEscaped(this char input)
 {
     using (var sw = new StringWriter())
         using (var codeProvider = new Microsoft.CSharp.CSharpCodeProvider())
         {
             codeProvider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(input), sw, null);
             return(sw.ToString());
         }
 }
Пример #13
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.WriteLine("do");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("} while (");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine(");");

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.WriteLine("Do");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("Loop Until ");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine();

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
Пример #14
0
        /// <summary>
        /// Gets the primitive value literal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string GetPrimitiveValueLiteral(object value)
        {
            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                provider.GenerateCodeFromExpression(new CodePrimitiveExpression(value), writer, null);

                return writer.ToString();
            }
        }
Пример #15
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(_condition, tw, opts);
                        tw.Write("?");
                        provider.GenerateCodeFromExpression(_truePart, tw, opts);
                        tw.Write(":");
                        provider.GenerateCodeFromExpression(_falsePart, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("If(");
                        provider.GenerateCodeFromExpression(_condition, tw, opts);
                        tw.Write(",");
                        provider.GenerateCodeFromExpression(_truePart, tw, opts);
                        tw.Write(",");
                        provider.GenerateCodeFromExpression(_falsePart, tw, opts);
                        tw.Write(")");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #16
0
 private void GenerateCS()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         CodeGeneratorOptions opts = new CodeGeneratorOptions();
         StringBuilder        sb   = new StringBuilder();
         using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString))
         {
             provider.GenerateCodeFromMember(Field, tw, opts);
             var type = new StringWriter();
             provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts);
             sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder());
         }
         Text = sb.ToString();
     }
 }
Пример #17
0
 private void GenerateCS()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         CodeGeneratorOptions opts = new CodeGeneratorOptions();
         StringBuilder sb = new StringBuilder();
         using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString))
         {
             provider.GenerateCodeFromMember(Field, tw, opts);
             var type = new StringWriter();
             provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts);
             sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder());
         }
         Text = sb.ToString();
     }
 }
Пример #18
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(Parameter, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    StringBuilder sb = new StringBuilder();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(sb), opts.IndentString))
                    {
                        provider.GenerateCodeFromExpression(Parameter, tw, opts);
                        switch (Option)
                        {
                        case ParamOption.Optional:
                            sb.Replace("ByVal " + Parameter.Name, "Optional ByVal " + Parameter.Name);
                            break;

                        case ParamOption.ParamArray:
                            sb.Replace("ByVal " + Parameter.Name, "ByVal ParamArray " + Parameter.Name);
                            break;

                        default:
                            throw new NotSupportedException(Option.ToString());
                        }
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #19
0
 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     provider.GenerateCodeFromExpression(Parameter, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 StringBuilder sb = new StringBuilder();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(sb), opts.IndentString))
                 {
                     provider.GenerateCodeFromExpression(Parameter, tw, opts);
                     switch (Option)
                     {
                         case ParamOption.Optional:
                             sb.Replace("ByVal " + Parameter.Name, "Optional ByVal " + Parameter.Name);
                             break;
                         case ParamOption.ParamArray:
                             sb.Replace("ByVal " + Parameter.Name, "ByVal ParamArray " + Parameter.Name);
                             break;
                         default:
                             throw new NotSupportedException(Option.ToString());
                     }
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
Пример #20
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write("(");

                            for (int i = 0; i < _params.Count; i++)
                            {
                                if (i > 0)
                                    tw.Write(", ");

                                if (_params[i].Type != null)
                                {
                                    provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                    tw.Write(" ");
                                }

                                tw.Write(_params[i].Name);
                            }

                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write(")");

                            tw.Write(" => ");
                            provider.GenerateCodeFromExpression(Expression, tw, opts);
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("Function(");

                            for (int i = 0; i < _params.Count; i++)
                            {
                                if (i > 0)
                                    tw.Write(", ");

                                tw.Write(_params[i].Name);

                                if (_params[i].Type != null)
                                {
                                    tw.Write(" As ");
                                    provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                }
                            }

                            tw.Write(") ");
                            provider.GenerateCodeFromExpression(Expression, tw, opts);
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
Пример #21
0
        //public CodeSwitchStatement Break()
        //{
        //    m_cases.Add(new CaseBlock());
        //    return this;
        //}

        #region ICustomCodeDomObject Members

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("switch (");
                        if (SwitchExpression != null)
                        {
                            provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                        }
                        tw.WriteLine(")");
                        tw.WriteLine("{");
                        tw.Indent++;
                        foreach (CaseBlock c in m_cases)
                        {
                            if (c.Statements != null)
                            {
                                if (c.CaseExpression != null)
                                {
                                    tw.Write("case ");
                                    provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                    tw.WriteLine(":");
                                }
                                else
                                {
                                    tw.WriteLine("default:");
                                }

                                tw.Indent++;
                                foreach (CodeStatement stmt in c.Statements)
                                {
                                    provider.GenerateCodeFromStatement(stmt, tw, opts);
                                }
                                tw.Indent--;
                            }
                            //else
                            //{
                            //    tw.Indent++;
                            //    tw.WriteLine("break;");
                            //    tw.Indent--;
                            //}
                        }
                        tw.Indent--;
                        tw.WriteLine("}");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("Select Case ");
                        if (SwitchExpression != null)
                        {
                            provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                        }
                        tw.WriteLine();
                        tw.Indent++;
                        foreach (CaseBlock c in m_cases)
                        {
                            if (c.Statements != null)
                            {
                                if (c.CaseExpression != null)
                                {
                                    tw.Write("Case ");
                                    provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                    tw.WriteLine();
                                }
                                else
                                {
                                    tw.WriteLine("Case Else");
                                }

                                tw.Indent++;
                                for (int i = 0; i < c.Statements.Length; i++)
                                {
                                    CodeStatement stmt = c.Statements[i];
                                    if (!(stmt is CodeExitSelectStatement) || i < c.Statements.Length - 1)
                                    {
                                        provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                }
                                tw.Indent--;
                            }
                            //else if (i < m_cases.Count - 1)
                            //{
                            //    tw.Indent++;
                            //    tw.WriteLine("Exit Select");
                            //    tw.Indent--;
                            //}
                        }
                        tw.Indent--;
                        tw.WriteLine("End Select");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #22
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("foreach (");
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" in ");
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("Next");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
Пример #23
0
        /// <summary>
        /// Returns a valid C# representation of a typed argument that can be 
        /// used in a custom attribute for code generation.
        /// </summary>
        public string ToCSharpString(CustomAttributeTypedArgument argument)
        {
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
            using (var writer = new System.IO.StringWriter(CultureInfo.InvariantCulture))
            {
                if (argument.Value == null)
                {
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(null), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                    return writer.ToString();
                }

                if (argument.ArgumentType == typeof(string))
                    return "\"" + argument.Value + "\"";

                if (argument.ArgumentType.IsEnum)
                    return argument.ArgumentType.Name + "." + Enum.GetName(argument.ArgumentType, argument.Value);

                if (argument.ArgumentType == typeof(Type))
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodeTypeOfExpression(((Type)argument.Value).Name), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                else
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(argument.Value), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());

                return writer.ToString();
            }
        }
Пример #24
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("lock (");
                        if (LockExpression != null)
                        {
                            provider.GenerateCodeFromExpression(LockExpression, tw, opts);
                        }
                        tw.WriteLine(")");
                        tw.WriteLine("{");
                        tw.Indent++;
                        if (Statements != null)
                        {
                            foreach (CodeStatement statement in Statements)
                            {
                                provider.GenerateCodeFromStatement(statement, tw, opts);
                            }
                        }
                        tw.Indent--;
                        tw.WriteLine("}");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("SyncLock ");
                        if (LockExpression != null)
                        {
                            provider.GenerateCodeFromExpression(LockExpression, tw, opts);
                        }
                        tw.WriteLine();
                        tw.Indent++;
                        if (Statements != null)
                        {
                            foreach (CodeStatement statement in Statements)
                            {
                                provider.GenerateCodeFromStatement(statement, tw, opts);
                            }
                        }
                        tw.Indent--;
                        tw.WriteLine("End SyncLock");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #25
0
 static string ToLiteral(string input)
 {
     var writer = new System.IO.StringWriter();
     Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
     provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(input), writer, null);
     return writer.GetStringBuilder().ToString();
 }
Пример #26
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("switch (");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine(":");
                                    }
                                    else
                                    {
                                        tw.WriteLine("default:");
                                    }

                                    tw.Indent++;
                                    foreach (CodeStatement stmt in c.Statements)
                                    {
                                        provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("break;");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("Select Case ");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("Case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine();
                                    }
                                    else
                                    {
                                        tw.WriteLine("Case Else");
                                    }

                                    tw.Indent++;
                                    for (int i = 0; i < c.Statements.Length; i++)
                                    {
                                        CodeStatement stmt = c.Statements[i];
                                        if (!(stmt is CodeExitSelectStatement) || i < c.Statements.Length-1)
                                            provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else if (i < m_cases.Count - 1)
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("Exit Select");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("End Select");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
Пример #27
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("foreach (");
                        provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                        tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName));
                        provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                        tw.WriteLine(")");
                        tw.WriteLine("{");
                        tw.Indent++;
                        if (Statements != null)
                        {
                            foreach (CodeStatement statement in Statements)
                            {
                                provider.GenerateCodeFromStatement(statement, tw, opts);
                            }
                        }
                        tw.Indent--;
                        tw.WriteLine("}");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName));
                        provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                        tw.Write(" in ");
                        provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                        tw.WriteLine();
                        tw.Indent++;
                        if (Statements != null)
                        {
                            foreach (CodeStatement statement in Statements)
                            {
                                provider.GenerateCodeFromStatement(statement, tw, opts);
                            }
                        }
                        tw.Indent--;
                        tw.WriteLine("Next");
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }
Пример #28
0
        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter sw = new StringWriter();
                provider.GenerateCodeFromMember(_property, sw, opts);
                StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
                string line = sr.ReadLine();
                while (string.IsNullOrEmpty(line))
                    line = sr.ReadLine();
                int idx = line.IndexOf(" " + _property.Name);

                StringBuilder sb = new StringBuilder();
                sb.Append(" this[");
                foreach (CodeParameterDeclarationExpression parameter in Parameters)
                {
                    provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts);
                    sb.Append(", ");
                }
                sb.Length -= 2;
                sb.Append("]");
                Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx+_property.Name.Length+1)).ToString();
            }
        }
Пример #29
0
        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
            case CodeDomGenerator.Language.CSharp:
                using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        if (_params.Count != 1 || _params[0].Type != null)
                        {
                            tw.Write("(");
                        }

                        for (int i = 0; i < _params.Count; i++)
                        {
                            if (i > 0)
                            {
                                tw.Write(", ");
                            }

                            if (_params[i].Type != null)
                            {
                                provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                tw.Write(" ");
                            }

                            tw.Write(_params[i].Name);
                        }

                        if (_params.Count != 1 || _params[0].Type != null)
                        {
                            tw.Write(")");
                        }

                        tw.Write(" => ");
                        provider.GenerateCodeFromExpression(Expression, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            case CodeDomGenerator.Language.VB:
                using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                {
                    System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                    using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                    {
                        tw.Write("Function(");

                        for (int i = 0; i < _params.Count; i++)
                        {
                            if (i > 0)
                            {
                                tw.Write(", ");
                            }

                            tw.Write(_params[i].Name);

                            if (_params[i].Type != null)
                            {
                                tw.Write(" As ");
                                provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                            }
                        }

                        tw.Write(") ");
                        provider.GenerateCodeFromExpression(Expression, tw, opts);
                        Value = tw.InnerWriter.ToString();
                    }
                }
                break;

            default:
                throw new NotImplementedException(language.ToString());
            }
        }