2011-10-23 149 views
1

一個全局變量,據我可以從website來講,下面的代碼應編譯成一個DLL具有頂級性能Version聲明中噓聲

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,其中放置了頂級靜態方法,但我不知道如何向其添加屬性)。

回答

0

在.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)