在这个 IronPython 示例中,@ 符号的作用是什么?它是如何实现的?

发布于 2024-10-05 01:19:06 字数 577 浏览 1 评论 0原文

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

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
   ";

Is that '@' part of C# or is that something added by IronPython? If the latter, how do you do that in C#, some kind of operator overloading (basically could I then make '@' do whatever I want, etc)? Example implementation would be great. Otherwise, what's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2024-10-12 01:19:06

@""逐字字符串在 C# 中。也就是说,其中的转义字符不会被解释。

在本例中,Python 代码存储在 C# code 字符串变量中,然后使用ScriptEngine(它本身是使用Python.CreateEngine()创建的)。

@"" is a verbatim string literal in C#. That is, escape characters inside it are not interpreted.

In this case, the python code is being stored in the C# code string variable, and then is compiled into a CompiledCode from a ScriptSource using a ScriptEngine (which is itself created using Python.CreateEngine()).

孤独岁月 2024-10-12 01:19:06

@ 是 C# 代码的一部分。这意味着该字符串是逐字字符串文字。该字符串保存要执行的Python代码。

The @ is part of the C# code. It means that the string is a verbatim string literal. The string holds the Python code to be executed.

夜唯美灬不弃 2024-10-12 01:19:06
string path = @"C:\Sweet\now\I\can\use\backslashes\without\writing\them\all\twice.txt"
string path = @"C:\Sweet\now\I\can\use\backslashes\without\writing\them\all\twice.txt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文