在 boo 中声明一个全局变量

发布于 2024-12-11 07:38:54 字数 584 浏览 3 评论 0原文

据我从 网站 得知,以下代码应该编译到具有顶级属性 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 技术交流群。

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

发布评论

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

评论(1

倥絔 2024-12-18 07:38:54

在 .net 中,无法拥有实际上不是类成员的方法或字段。 Boo 通过在模块中拥有主文件的隐式类(正如您所注意到的)来隐藏这一点,但在导入时您仍然需要作为成员访问它。

对于静态,您必须首先引用类型,然后引用成员,因此在您的示例中打印版本将如下所示:

import MyLibrary
print (MainClass.Version)

当然,这不是在 .net 中存储版本信息的“正确”方法,而是使用程序集级别属性来代替。看起来更像是这样的:

[assembly: System.Reflection.AssemblyVersion("1.0")]

namespace MyLibrary

[Module]
class MainClass:
  pass

然后获取您将使用反射执行的版本,有几种方法来获取程序集,但最简单的方法是获取类型,然后获取程序集:

import System
import System.Reflection
import MyLibrary

atts = typeof(MainClass).Assembly \
         .GetCustomAttributes(typeof(AssemblyVersionAttribute), false)

version = (atts[0] as AssemblyVersionAttribute).Version
print(version)

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:

import MyLibrary
print (MainClass.Version)

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:

[assembly: System.Reflection.AssemblyVersion("1.0")]

namespace MyLibrary

[Module]
class MainClass:
  pass

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:

import System
import System.Reflection
import MyLibrary

atts = typeof(MainClass).Assembly \
         .GetCustomAttributes(typeof(AssemblyVersionAttribute), false)

version = (atts[0] as AssemblyVersionAttribute).Version
print(version)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文