在这个IronPython示例中,@符号正在做什么以及如何实现?

时间:2010-11-29 20:46:51

标签: c# ironpython

http://www.ironpython.info/index.php/Using_Python_Classes_from_.NET/CSharp_IP_2.6

   string code = @"
   print 'test = ' + test
   class MyClass:
       def __init__(self):
           pass

       def somemethod(self):
           print 'in some method'

       def isodd(self, n):
           return 1 == n % 2
   ";

这是'@'C#的一部分还是IronPython添加的东西?如果是后者,你如何在C#中做到这一点,某种运算符重载(基本上我可以让'@'做我想做的事情等等)?示例实现会很棒。否则,这里发生了什么?

3 个答案:

答案 0 :(得分:6)

@""是C#中的verbatim string literal。也就是说,它内部的转义字符不会被解释。

在这种情况下,python代码存储在C#code字符串变量中,然后使用CompiledCodeScriptSource编译为ScriptEngine(它本身是使用Python.CreateEngine())创建的。

答案 1 :(得分:0)

@是C#代码的一部分。这意味着该字符串是verbatim string literal。该字符串包含要执行的Python代码。

答案 2 :(得分:0)

string path = @"C:\Sweet\now\I\can\use\backslashes\without\writing\them\all\twice.txt"
相关问题