Example #1
0
        protected override void UpdateValue()
        {
            CodeDomProvider provider = base.GetProvider();
            string          expr     = "(";

            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(Left, writer, new CodeGeneratorOptions());
                expr += writer.ToString();
                expr += ")";
            }

            if (_shiftLeft)
            {
                expr += " << ";
            }
            else
            {
                expr += " >> ";
            }

            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(Right, writer, new CodeGeneratorOptions());
                expr += string.Format("({0})", writer.ToString());
            }

            Value = expr;
        }
Example #2
0
        private void tv_Elements_MouseMove(object sender, MouseEventArgs e)
        {
            if (tv_Elements.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed && e.OriginalSource.GetType() == typeof(TextBlock))
            {
                XmlElement  element = tv_Elements.SelectedItem as XmlElement;
                SapCompInfo ci      = new SapCompInfo();
                ci.Id         = element.GetAttribute("id");
                ci.Type       = element.GetAttribute("type");
                ci.Name       = element.GetAttribute("name");
                ci.FindMethod = ci.FindByNameCode();

                CodeDomProvider      provider = CodeDomProvider.CreateProvider("c#");
                CodeGeneratorOptions options  = new CodeGeneratorOptions();
                options.BracingStyle = "C";
                StringBuilder sb = new StringBuilder();

                try
                {
                    using (TextWriter sourceWriter = new StringWriter(sb))
                    {
                        provider.GenerateCodeFromExpression(ci.FindMethod, sourceWriter, options);
                    }
                    DragDrop.DoDragDrop(tv_Elements, sb.ToString(), DragDropEffects.Copy);
                    e.Handled = true;
                }
                catch
                {
                }
            }
        }
Example #3
0
 private string CodeToString(CodeExpression expr)
 {
     using (TextWriter tx = new StringWriter()) {
         provider.GenerateCodeFromExpression(expr, tx, new CodeGeneratorOptions());
         return(tx.ToString());
     }
 }
Example #4
0
        protected override void UpdateValue()
        {
            CodeDomProvider provider = GetProvider();
            string          prefix   = null;

            switch (this.LanguageType)
            {
            case Transform.LanguageType.CSharp:
                prefix = "! (";
                break;

            case Transform.LanguageType.VisualBasic:
                prefix = "Not (";
                break;

            default:
                ThrowInvalidEnumValue(this.LanguageType);
                return;
            }

            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(Expression, writer, new CodeGeneratorOptions());
                Value = prefix + writer.ToString() + ")";
            }
        }
Example #5
0
        protected override void UpdateValue()
        {
            CodeDomProvider provider = base.GetProvider();
            string          expr     = null;

            switch (_direction)
            {
            case FieldDirection.Out:
                expr = "out ";
                break;

            case FieldDirection.Ref:
                expr = "ref ";
                break;

            default:
                expr = string.Empty;
                break;
            }

            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(_symbolExpr, writer, new CodeGeneratorOptions());
                expr += writer.ToString();
            }

            Value = expr;
        }
Example #6
0
        private string GetTypeName(Type type, bool lastOnly)
        {
            string                      name                    = "";
            CodeDomProvider             codeDomProvider         = CodeDomProvider.CreateProvider("C#");
            CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression(new CodeTypeReference(type));

            using (StringWriter writer = new StringWriter())
            {
                codeDomProvider.GenerateCodeFromExpression(typeReferenceExpression, writer, new CodeGeneratorOptions());
                name = writer.GetStringBuilder().ToString();
            }
            if (lastOnly)
            {
                int index = name.LastIndexOf('.');
                if (index == -1)
                {
                    return(name);
                }
                index++;
                int length = name.Length - index;

                return(name.Substring(index, length));
            }
            else
            {
                return(name);
            }
        }
Example #7
0
        /// <summary>
        /// Gets the friendly name of the given type.
        /// </summary>
        public static string GetFriendlyName(this Type src)
        {
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }

            using CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
            var typeReferenceExpression = new CodeTypeReferenceExpression(new CodeTypeReference(src));

            using var writer = new StringWriter();

            codeDomProvider.GenerateCodeFromExpression(typeReferenceExpression, writer, new CodeGeneratorOptions());

            //
            // Ezt a nevet meg nem lehet eleresi utvonalakban hasznalni (tartalmaz "<" es ">" karaktereket).
            //

            string unsafeName = writer.GetStringBuilder().ToString();

            return(Replacer.Replace(unsafeName, m => m.Groups[0].Value switch
            {
                "<" => "{",
                ">" => "}",
                _ => throw new NotImplementedException()
            }));
Example #8
0
        /// <summary>
        /// 转为文字,比如''转为'
        /// </summary>
        /// <param name="value">要转换的字符串</param>
        /// <returns>转换后的文字</returns>
        public static string ToLiteral(this string value)
        {
            CodeDomProvider cdp = CodeDomProvider.CreateProvider("CSharp");
            StringWriter    sw  = new StringWriter();

            cdp.GenerateCodeFromExpression(new CodePrimitiveExpression(value), sw, null);
            return(sw.ToString());
        }
 // 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());
     }
 }
Example #10
0
            public void GenerateDeclaration(TextWriter tw, CodeDomProvider provider, CodeGeneratorOptions opts, CodeTypeReference returnType, CodeParameterDeclarationExpression[] parameters)
            {
                tw.Write(string.Format("public static {0} operator ", OperatorName));
                provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(returnType), tw, opts);
                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.WriteLine(")");
            }
Example #11
0
        private static string Serialize(CodeExpression expression)
        {
            CodeDomProvider provider     = CodeDomProvider.CreateProvider("C#");
            StringWriter    stringWriter = new StringWriter();

            provider.GenerateCodeFromExpression(expression, stringWriter, new CodeGeneratorOptions());
            return(stringWriter.ToString());
        }
Example #12
0
 public static string escape(string input)
 {
     using (StringWriter writer = new StringWriter()) {
         using (CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp")) {
             provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
             return(writer.ToString());
         }
     }
 }
Example #13
0
        protected override void UpdateValue()
        {
            CodeDomProvider provider = GetProvider();

            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(Expression, writer, new CodeGeneratorOptions());
                Value = "-" + writer.ToString();
            }
        }
        private string GetTypeName(Type type)
        {
            var typeReferenceExpression = new CodeTypeReferenceExpression(new CodeTypeReference(type));

            using (var writer = new StringWriter())
            {
                _codeDomProvider.GenerateCodeFromExpression(typeReferenceExpression, writer, new CodeGeneratorOptions());
                return(writer.GetStringBuilder().ToString());
            }
        }
Example #15
0
        private static string GetTypeAsString(Type type)
        {
            CodeDomProvider             codeDomProvider         = CodeDomProvider.CreateProvider("C#");
            CodeTypeReference           typeReference           = new CodeTypeReference(type);
            CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression(typeReference);

            using (StringWriter typeStringWriter = new StringWriter()) {
                codeDomProvider.GenerateCodeFromExpression(typeReferenceExpression, typeStringWriter, new CodeGeneratorOptions());
                return(typeStringWriter.GetStringBuilder().ToString());
            }
        }
Example #16
0
 /// <summary>
 /// 生成类型
 /// </summary>
 /// <param name="expression">表达式</param>
 /// <returns>代码结构</returns>
 public static string GenerateExpression(CodeExpression expression)
 {
     using (var sw = new StringWriter(new StringBuilder()))
     {
         codeDomProvider.GenerateCodeFromExpression(expression, sw, new CodeGeneratorOptions
         {
             ElseOnClosing = true,
         });
         return(sw.ToString());
     }
 }
Example #17
0
    private static void WriteLine(StreamWriter writer, CodeDomProvider provider, string line)
    {
        writer.Write("            ");
        if (line.StartsWith("\t"))
        {
            writer.Write("@");
        }

        provider.GenerateCodeFromExpression(new CodePrimitiveExpression(line), writer, null);
        writer.WriteLine(",");
    }
Example #18
0
File: CJ.cs Project: nistck/Jx
        private static string GetCSharpStringOf(Type type)
        {
            CodeDomProvider             codeDomProvider = CodeDomProvider.CreateProvider("C#");
            CodeTypeReferenceExpression expression      = new CodeTypeReferenceExpression(new CodeTypeReference(type));
            string result;

            using (StringWriter stringWriter = new StringWriter())
            {
                codeDomProvider.GenerateCodeFromExpression(expression, stringWriter, new CodeGeneratorOptions());
                result = stringWriter.GetStringBuilder().ToString();
            }
            return(result);
        }
Example #19
0
        /// <summary>
        ///     Gets the <see cref="NRParameter" /> as a C# string.
        /// </summary>
        /// <param name="parameter">The parameter to get the code for.</param>
        /// <returns>A string representing the parameter.</returns>
        public static string Declaration(this NRParameter parameter)
        {
            string extensionParam = parameter.IsExtensionParameter ? "this " : "";
            string attributes     = parameter.Attributes.Aggregate("", (current, nrAttribute) => current + nrAttribute.Declaration( ));

            attributes = AddSpace(attributes);

            string modifier = AddSpace(parameter.ParameterModifier.Declaration( ));

            string type = parameter.Type.Declaration( );

            if (type.EndsWith("&"))
            {
                type = type.Substring(0, type.Length - 1);
            }

            string defaultValue = "";

            if (!string.IsNullOrWhiteSpace(parameter.DefaultValue))
            {
                if (parameter.TypeFullName == typeof(string).FullName)
                {
                    using (StringWriter writer = new StringWriter( ))
                    {
                        using (CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"))
                        {
                            provider.GenerateCodeFromExpression(new CodePrimitiveExpression(parameter.DefaultValue), writer, null);
                            defaultValue = " = " + writer;
                        }
                    }
                }
                else if (parameter.TypeFullName == typeof(char).FullName)
                {
                    using (StringWriter writer = new StringWriter( ))
                    {
                        using (CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"))
                        {
                            provider.GenerateCodeFromExpression(new CodePrimitiveExpression(parameter.DefaultValue[0]), writer, null);
                            defaultValue = " = " + writer;
                        }
                    }
                }
                else
                {
                    defaultValue = " = " + parameter.DefaultValue;
                }
            }

            return(string.Format("{0}{1}{2}{3} {4}{5}", attributes, extensionParam, modifier, type, parameter.Name, defaultValue));
        }
Example #20
0
        public static void ShowTypeReferenceExpression()
        {
            //<Snippet3>
            // Creates an expression referencing the System.DateTime type.
            CodeTypeReferenceExpression typeRef1 = new CodeTypeReferenceExpression("System.DateTime");

            // Create a C# code provider
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            // Generate code and send the output to the console
            provider.GenerateCodeFromExpression(typeRef1, Console.Out, new CodeGeneratorOptions());
            // The code generator produces the following source code for the preceeding example code:

            //    System.DateTime

            //</Snippet3>
        }
Example #21
0
        public static string ToStringLiteral(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return("");
            }

            if (input.StartsWith("`"))
            {
                using (StringWriter writer = new StringWriter())
                {
                    s_provider.GenerateCodeFromExpression(new CodePrimitiveExpression(RemoveSurrounding(input, "`", "`")), writer, s_generatorOptions);
                    return(writer.ToString());
                }
            }

            return(input);
        }
Example #22
0
        public static void ShowTypeReference()
        {
            //<Snippet2>
            // Creates a reference to the System.DateTime type.
            CodeTypeReference typeRef1 = new CodeTypeReference("System.DateTime");

            // Creates a typeof expression for the specified type reference.
            CodeTypeOfExpression typeof1 = new CodeTypeOfExpression(typeRef1);

            // Create a C# code provider
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            // Generate code and send the output to the console
            provider.GenerateCodeFromExpression(typeof1, Console.Out, new CodeGeneratorOptions());
            // The code generator produces the following source code for the preceeding example code:
            //    typeof(System.DateTime)
            //</Snippet2>
        }
Example #23
0
        public string PrimitiveToLiteral(object input)
        {
            using (var writer = new StringWriter())
            {
                provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
                var result = writer.ToString();

                // Remove trailing 'f' from float
                if (input is float && !string.IsNullOrEmpty(result))
                {
                    result = result.ToLower();
                    if (result.EndsWith("f"))
                    {
                        result = result.Substring(0, result.Length - 1);
                    }
                }

                return(result);
            }
        }
        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);
        }
Example #25
0
        private string Serialize(CodeExpression codeExpression)
        {
            StringWriter    stringWriter = new StringWriter();
            CodeDomProvider provider     = CodeDomProvider.CreateProvider("C#");

            provider.GenerateCodeFromExpression(codeExpression, stringWriter, new CodeGeneratorOptions());
            String expression = stringWriter.ToString().Trim().Replace(stringWriter.NewLine, " ");

            int startRemove = -1;
            int removeCount = 0;

            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] == ' ')
                {
                    if (startRemove == -1)
                    {
                        startRemove = i;
                    }
                    else
                    {
                        removeCount++;
                    }
                }
                else
                {
                    if (removeCount != 0)
                    {
                        expression  = expression.Remove(startRemove, removeCount);
                        removeCount = 0;
                    }

                    startRemove = -1;
                }
            }

            return(expression);
        }
Example #26
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="InputString"></param>
        /// <param name="LanguageProvider"></param>
        /// <returns></returns>
        public static string ToLiteral(string InputString, string LanguageProvider)
        {
            string str = string.Empty;

            try
            {
                using (StringWriter writer = new StringWriter())
                {
                    using (CodeDomProvider provider = CodeDomProvider.CreateProvider(LanguageProvider))
                    {
                        provider.GenerateCodeFromExpression(new CodePrimitiveExpression(InputString), writer, null);
                        str = writer.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                str = $"Message:  {e.Message}{Environment.NewLine}" +
                      $"Source:  {e.Source}{Environment.NewLine}" +
                      $"StackTrace:  {e.StackTrace}{Environment.NewLine}";
            }
            return(str);
        }
Example #27
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());
        }
Example #28
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());
        }
Example #29
0
 public void GenerateCodeFromExpression(CodeExpression e, TextWriter w, CodeGeneratorOptions o)
 {
     provider.GenerateCodeFromExpression(e, w, o);
 }
 protected override void ProcessRecord()
 {
     if (CompileUnit != null && CompileUnit.Length > 0)
     {
         foreach (CodeCompileUnit compileUnit in CompileUnit)
         {
             if (compileUnit != null)
             {
                 _provider.GenerateCodeFromCompileUnit(compileUnit, _indentedWriter, Options);
             }
         }
     }
     else if (Expression != null && Expression.Length > 0)
     {
         foreach (CodeExpression expr in Expression)
         {
             if (expr != null)
             {
                 _provider.GenerateCodeFromExpression(expr, _indentedWriter, Options);
             }
         }
     }
     else if (Member != null && Member.Length > 0)
     {
         foreach (CodeTypeMember member in Member)
         {
             if (member != null)
             {
                 _provider.GenerateCodeFromMember(member, _indentedWriter, Options);
             }
         }
     }
     else if (NS != null && NS.Length > 0)
     {
         foreach (CodeNamespace ns in NS)
         {
             if (ns != null)
             {
                 _provider.GenerateCodeFromNamespace(ns, _indentedWriter, Options);
             }
         }
     }
     else if (Statement != null && Statement.Length > 0)
     {
         foreach (CodeStatement st in Statement)
         {
             if (st != null)
             {
                 _provider.GenerateCodeFromStatement(st, _indentedWriter, Options);
             }
         }
     }
     else if (Type != null && Type.Length > 0)
     {
         foreach (CodeTypeDeclaration t in Type)
         {
             if (t != null)
             {
                 _provider.GenerateCodeFromType(t, _indentedWriter, Options);
             }
         }
     }
 }