本文介绍了CodeDOM的和字符串处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了这一点,但无法找到任何坚实想看看如果有人能在正确的方向指向我。我想看看是否CodeDom中可以处理不同语言之间的字符串和concantination,没有我设置每种语言条件的字符串。



例如,我需要生成以下的完全按照如下所示的在C#和VB.NET通过CodeDom中:



C#

  ERRORMSG = errorMsg.Replace('','\'')。更换(\r\\\
,@\ N);
System.Windows.Browser.HtmlPage.Window.Eval(抛出新的错误(\未处理的错误在Silverlight应用程序+ + ERRORMSG\););

VB.NET

  ERRORMSG = errorMsg.Replace(CC).Replace(CHRW(13)及CHRW(10),\\\




CodeMethodInvokeExpression errorMsg.Replace System.Windows.Browser.HtmlPage.Window.Eval 是很简单的,这里面他们的字符串,我想不通,如果CodeDom中可以自动处理。


解决方案

可惜码元,结合时,并不总是产生预期的效果,因为供应商将采取一定的自由解释意图。解决这个问题的办法是使用。



下面的代码(VB.NET和C#)的作品产生,你在你的问题中列出的评估和演示陈述。随意使用哪个最适合你的:



VB.NET版本



 进口System.CodeDom 
进口System.CodeDom.Compiler
进口的System.Reflection
进口System.Text
进口System.IO
进口Microsoft.CSharp

类公共PrintEvalStatement
公共提供商作为CodeDomProvider

子新()
昏暗留给新CodePrimitiveExpression(抛出新错误(在Silverlight应用程序未处理的错误)
昏暗的中间作为新CodeVariableReferenceExpression(ERRORMSG)
尺寸权作为新CodePrimitiveExpression();)

昏暗targetObject =新CodeTypeReferenceExpression(System.Windows.Browser.HtmlPage.Window)
暗淡方法名=评估和演示

提供商=新VBCodeProvider()
昏暗vbStatement作为字符串= ConcatStatement(左,中,右,targetObject,方法名)

提供商=新CSharpCodeProvider()
昏暗csStatement的String = ConcatStatement(左,中,右,targetObject,方法名)

Console.WriteLine(vbStatement)
Console.WriteLine(csStatement)
到Console.ReadLine()
端子

专用功能ConcatStatement(BYVAL如左图CodePrimitiveExpression,
BYVAL中间作为CodeVariableReferenceExpression,
BYVAL权CodePrimitiveExpression,
BYVAL targetObject作为CodeTypeReferenceExpression,
BYVAL方法名作为字符串)作为字符串
昏暗evalMessage作为新CodeExpression
evalMessage = ConcatString(左,中,右)

尺寸的eval作为新CodeMethodInvokeExpression(targetObject,方法名,evalMessage)
昏暗evalStatement作为新CodeExpressionStatement(EVAL)
尺寸SW作为StringWriter的=新的StringWriter()

使用TX作为的TextWriter =新的StringWriter()
provider.GenerateCodeFromStatement(evalStatement,TX,新CodeGeneratorOptions())
返回TX的ToString()
端使用
端功能
专用功能ConcatString(BYVAL如左图CodeExpression,
BYVAL中间作为CodeExpression,
BYVAL权CodeExpression)由于CodeExpression
返回新CodeSnippetExpression(CodeToString(左)+++ CodeToString(中)+++ CodeToString(右))
端功能
专用功能CodeToString(BYVAL EXPR作为CodeExpression)作为字符串
使用TX作为的TextWriter =新的StringWriter()
provider.GenerateCodeFromExpression(表达式,TX,新CodeGeneratorOptions())
返回tx.ToString()
端使用
端功能


端类

C#版本

 使用System.CodeDom; 
使用System.CodeDom.Compiler;
使用的System.Reflection;
使用System.Text;
:使用System.IO;
使用Microsoft.CSharp;
使用Microsoft.VisualBasic程序;

命名空间的CodeDOM
{
类节目
{
静态CodeDomProvider供应商;

静态无效的主要(字串[] args)
{
计划壳=新计划();
提供商=新VBCodeProvider();
CodePrimitiveExpression左=新CodePrimitiveExpression(抛出新的错误(\未处理的错误在Silverlight Application\));
CodeVariableReferenceExpression中间=新CodeVariableReferenceExpression(ERRORMSG);
CodePrimitiveExpression右=新CodePrimitiveExpression(\););

CodeTypeReferenceExpression targetObject =新CodeTypeReferenceExpression(System.Windows.Browser.HtmlPage.Window);
字符串方法名=评估和演示;

串vbStatement = shell.ConcatStatement(左,中,右,targetObject,方法名);

提供商=新CSharpCodeProvider();

串csStatement = shell.ConcatStatement(左,中,右,targetObject,方法名);

Console.WriteLine(vbStatement);
Console.WriteLine(csStatement);
到Console.ReadLine();

}

公共字符串ConcatStatement(CodePrimitiveExpression左,中间CodeVariableReferenceExpression,CodePrimitiveExpression权,CodeTypeReferenceExpression targetObject,字符串methodName中)
{
CodeExpression evalMessage =新CodeExpression();
evalMessage = ConcatString(左,中,右);

CodeMethodInvokeExpression EVAL =新CodeMethodInvokeExpression(targetObject,方法名,evalMessage);
CodeExpressionStatement evalStatement =新CodeExpressionStatement(EVAL);
使用(的TextWriter TX =新的StringWriter())
{
provider.GenerateCodeFromStatement(evalStatement,TX,新CodeGeneratorOptions());
返回tx.ToString();
}
}

私人CodeExpression ConcatString(CodeExpression左,CodeExpression中间,CodeExpression右){
返回新CodeSnippetExpression(CodeToString(左)+++ CodeToString (中间)+++ CodeToString(右));
}

私人字符串CodeToString(CodeExpression表达式){使用(的TextWriter TX =新的StringWriter()){
provider.GenerateCodeFromExpression(表达式,TX,新CodeGeneratorOptions
());
返回tx.ToString();
}
}
}
}


I've researched on this but couldn't find anything solid and wanted to see if someone can point me in the right direction. I'm trying to see if Codedom can handle strings and concantination between different languages, without me setting up conditional strings per language.

For example, I need to generate the following exactly as shown below in both C# and VB.NET via Codedom:

C#

errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");  

VB.NET

errorMsg = errorMsg.Replace(""""c, "'"c).Replace(ChrW(13) & ChrW(10), "\n")
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " + errorMsg + """);")

The CodeMethodInvokeExpression for errorMsg.Replace and System.Windows.Browser.HtmlPage.Window.Eval is simple enough, it's the string inside them that I can't figure out if Codedom can automatically handle.

解决方案

Unfortunately code primitives, when combined, don’t always produce desired results because the provider will take certain liberties to interpret intent. The way around this is to use a CodeSnippetExpression.

Here’s code (VB.NET & C#) that works to produce the Eval statements that you listed in your question. Feel free to use whichever works best for you:

VB.NET Version

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.Text
Imports System.IO
Imports Microsoft.CSharp

Public Class PrintEvalStatement
    Public provider As CodeDomProvider

    Sub New()
        Dim left As New CodePrimitiveExpression("throw new Error(""Unhandled Error in Silverlight Application ")
        Dim middle As New CodeVariableReferenceExpression("errorMsg")
        Dim right As New CodePrimitiveExpression(""");")

        Dim targetObject = New CodeTypeReferenceExpression("System.Windows.Browser.HtmlPage.Window")
        Dim methodName = "Eval"

        provider = New VBCodeProvider()
        Dim vbStatement As String = ConcatStatement(left, middle, right, targetObject, methodName)

        provider = New CSharpCodeProvider()
        Dim csStatement As String = ConcatStatement(left, middle, right, targetObject, methodName)

        Console.WriteLine(vbStatement)
        Console.WriteLine(csStatement)
        Console.ReadLine()
    End Sub

    Private Function ConcatStatement(ByVal left As CodePrimitiveExpression,
                                     ByVal middle As CodeVariableReferenceExpression,
                                     ByVal right As CodePrimitiveExpression,
                                     ByVal targetObject As CodeTypeReferenceExpression,
                                     ByVal methodName As String) As String
        Dim evalMessage As New CodeExpression
        evalMessage = ConcatString(left, middle, right)

        Dim eval As New CodeMethodInvokeExpression(targetObject, methodName, evalMessage)
        Dim evalStatement As New CodeExpressionStatement(eval)
        Dim sw As StringWriter = New StringWriter()

        Using tx As TextWriter = New StringWriter()
            provider.GenerateCodeFromStatement(evalStatement, tx, New CodeGeneratorOptions())
            Return tx.ToString()
        End Using
    End Function
    Private Function ConcatString(ByVal left As CodeExpression,
                                  ByVal middle As CodeExpression,
                                  ByVal right As CodeExpression) As CodeExpression
        Return New CodeSnippetExpression(CodeToString(left) + " + " + CodeToString(middle) + " + " + CodeToString(right))
    End Function
    Private Function CodeToString(ByVal expr As CodeExpression) As String
        Using tx As TextWriter = New StringWriter()
            provider.GenerateCodeFromExpression(expr, tx, New CodeGeneratorOptions())
            Return tx.ToString()
        End Using
    End Function


End Class

C# Version

using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
using System.IO;
using Microsoft.CSharp;
using Microsoft.VisualBasic;

namespace CodeDom
{
    class Program
    {       
        static CodeDomProvider provider;

        static void Main(string[] args)
        {
            Program shell = new Program();
            provider = new VBCodeProvider();
            CodePrimitiveExpression left = new CodePrimitiveExpression("throw new Error(\"Unhandled Error in Silverlight Application\")");
            CodeVariableReferenceExpression middle = new CodeVariableReferenceExpression("errorMsg");
            CodePrimitiveExpression right = new CodePrimitiveExpression("\");");

            CodeTypeReferenceExpression targetObject = new CodeTypeReferenceExpression("System.Windows.Browser.HtmlPage.Window");
            string methodName = "Eval";

            string vbStatement =  shell.ConcatStatement(left, middle, right, targetObject, methodName);

            provider = new CSharpCodeProvider();

            string csStatement = shell.ConcatStatement(left, middle, right, targetObject, methodName);

            Console.WriteLine(vbStatement);
            Console.WriteLine(csStatement);
            Console.ReadLine();

        }

        public string ConcatStatement(CodePrimitiveExpression left, CodeVariableReferenceExpression middle, CodePrimitiveExpression right, CodeTypeReferenceExpression targetObject, string methodName)
        {
            CodeExpression evalMessage = new CodeExpression();
            evalMessage = ConcatString(left, middle, right);

            CodeMethodInvokeExpression eval = new CodeMethodInvokeExpression(targetObject, methodName, evalMessage);
            CodeExpressionStatement evalStatement = new CodeExpressionStatement(eval);
            using (TextWriter tx = new StringWriter())
            {
                provider.GenerateCodeFromStatement(evalStatement, tx, new CodeGeneratorOptions());
                return tx.ToString();
            }
        }

        private CodeExpression ConcatString(CodeExpression left, CodeExpression middle, CodeExpression right) {
            return new CodeSnippetExpression(CodeToString(left) + " + " + CodeToString(middle) + " + " + CodeToString(right));
        }

        private string CodeToString(CodeExpression expr) {
            using (TextWriter tx = new StringWriter()) {
                provider.GenerateCodeFromExpression(expr,tx, new CodeGeneratorOptions());
                return tx.ToString();
            }
        }
    }
}

这篇关于CodeDOM的和字符串处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:40