我对此进行了研究,但找不到任何可靠的东西,想看看是否有人可以为我指出正确的方向。我正在尝试查看 Codedom 是否可以处理不同语言之间的字符串和连接,而无需我为每种语言设置条件字符串。
例如,我需要通过 Codedom 在 C# 和 VB.NET 中生成以下完全如下所示的内容:
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 + """);")
<用于 errorMsg.Replace
和 System.Windows.Browser.HtmlPage.Window.Eval
的 code>CodeMethodInvokeExpression 非常简单,我可以使用它们内部的字符串不知道Codedom是否可以自动处理。
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.
发布评论
评论(4)
不幸的是,代码原语在组合时并不总是产生期望的结果,因为提供者将采取一定的自由来解释意图。解决此问题的方法是使用 CodeSnippetExpression。
下面的代码(VB.NET 和 C#)可用于生成您在问题中列出的 Eval 语句。请随意使用最适合您的版本:
VB.NET 版本
C# 版本
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
C# Version
更新:
我刚刚尝试了以下代码:
它打印出:
在我看来,这像是 VB 版本和 C# 版本。对于假括号,您无能为力,但不会造成任何伤害。
Update:
I just tried the following code:
It prints out:
Which looks to me like a VB version and a C# version. Not a whole lot you can do about the spurious parentheses, but shouldn't cause any harm.
您可以调用 string.Concat 而不是使用 + 运算符,除非您需要生成与所示完全相同的代码。
输出 C#:
VB:
You could call string.Concat instead of using the + operator unless you need to generate the code exactly as shown.
Outputs C#:
VB:
类
CodeTypeReference
应该可以帮助您( http://msdn.microsoft.com/en-us/library/system.codedom.codetypereference.aspx),就像艾莉森使用的那样。The class
CodeTypeReference
should help you (http://msdn.microsoft.com/en-us/library/system.codedom.codetypereference.aspx), just as Alison used.