C#在字符串中执行代码总是会产生错误

时间:2015-06-11 16:53:04

标签: c#

我正在尝试制作一个类似的命令系统 每个命令都有一个字符串操作:

def sqr(a):
    time.sleep(0.2)
    print 'local {}'.format(os.getpid())
    if a == 20:
        raise Exception('fff')
    return a * a

pool = Pool(processes=4)


r = [pool.apply_async(sqr, (x,)) for x in range(100)]

pool.close()

# Without timeout, cannot respond to KeyboardInterrupt.
# Also need get to raise the exceptions workers may throw.
for item in r:
    item.get(timeout=999999)

# I don't think I need join since I already get everything.
pool.join()

print 'main {}'.format(os.getpid())

并阅读我使用此方法的代码:

     CommandReader.commands.Add(new CommandReader("MESSAGE", @"
        using System;

        namespace HelloWorld
        {
/// <summary>
/// Summary description for Class1.
/// </summary>
class HelloWorldClass
{
            static void Main(string[] args)
            {
                Console.WriteLine("+"Hello World!"+@");
                Console.ReadLine();
            }
 }
   }

        "));

我总是在CompilerResults中遇到错误 任何人都可以告诉我我的代码有什么问题

2 个答案:

答案 0 :(得分:2)

Console.WriteLine("+"Hello World!"+@");替换为Console.WriteLine(""Hello World!"");""转义"

答案 1 :(得分:0)

由于您连接字符串的方式不正确,您的源代码无法编译。它应该是这样的:

string action = @"
using System;

    namespace HelloWorld
    {

        class HelloWorldClass
        {
              static void Main(string[] args)
              {
                   //pay attention to the @ next to hello world
                   Console.WriteLine(""" + @"Hello World!""" + @");
                   Console.ReadLine();
              }
        }
     }
";