在 boo 中声明一个全局变量
据我从 网站 得知,以下代码应该编译到具有顶级属性 Version
的 DLL:
namespace MyLibrary
[Module]
class MainClass:
public static Version as string
static def constructor():
Version = "0.1"
这可以编译,但如果我随后将这些命令输入 booish
:
import MyLibrary
print (Version)
那么我会得到“错误:未知标识符:'版本' ”。
据推测,这段代码可以在该语言的早期版本中运行。我正在使用0.9.4.9。达到这种效果的正确方法是什么?
(我注意到有一个隐式静态类 MyVersionModule
其中放置了顶级静态方法,但我也不知道如何向其添加属性)。
As far as I can tell from the website, the following code should compile to a DLL with a top-level property Version
:
namespace MyLibrary
[Module]
class MainClass:
public static Version as string
static def constructor():
Version = "0.1"
This compiles, but if I then enter these commands into booish
:
import MyLibrary
print (Version)
then I get "ERROR: Unknown identifier: 'Version'".
Presumably this code worked in an earlier version of the language. I am using 0.9.4.9. What is the correct way to achieve this effect?
(I've noticed that there is an implicit static class MyVersionModule
in which top-level static methods get placed, but I don't know how to add properties to this either).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 .net 中,无法拥有实际上不是类成员的方法或字段。 Boo 通过在模块中拥有主文件的隐式类(正如您所注意到的)来隐藏这一点,但在导入时您仍然需要作为成员访问它。
对于静态,您必须首先引用类型,然后引用成员,因此在您的示例中打印版本将如下所示:
当然,这不是在 .net 中存储版本信息的“正确”方法,而是使用程序集级别属性来代替。看起来更像是这样的:
然后获取您将使用反射执行的版本,有几种方法来获取程序集,但最简单的方法是获取类型,然后获取程序集:
In .net there is no way to have methods or fields that are not actually members of a class. Boo hides this somewhat by having the implicit class for the main file in the module (as you noticed) but when importing you still need to access it as a member.
For statics you have to first reference the type then the member so in your example printing the version would be like this:
Of course this isn't the 'correct' way to store version information in .net though, which is to use assembly level attributes instead. That would look more like this:
Then getting the version you would do using reflection, there are a couple of ways to get the assembly but the easiest is to get the Type then it's assembly: