【问题标题】:In this IronPython example what is the @ symbol doing and how is it implemented?在这个 IronPython 示例中,@ 符号在做什么以及它是如何实现的?
【发布时间】:2010-11-30 04:55:25
【问题描述】:

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# 中做到这一点,某种运算符重载(基本上我可以让'@'做任何我想做的事情,等等)?示例实现会很棒。不然怎么回事?

【问题讨论】:

    标签: c# ironpython


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:
        string path = @"C:\Sweet\now\I\can\use\backslashes\without\writing\them\all\twice.txt"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-31
          • 2021-11-12
          • 1970-01-01
          • 2016-09-08
          • 1970-01-01
          • 2022-11-03
          相关资源
          最近更新 更多