1、Small Basic 第三方扩展库开发指南此文由 Small Basic 中文站-快乐编程翻译。Small Basic 编译器支持第三方类库作为插件的方式加载,这样就可以增加自己感兴趣的扩展库了。此文来自 SmallBasic.Cn (Small Basic 中文站-快乐编程) 转载请注明出处开发的类库必须基于.Net 语言并且编译为.Net 程序集。以下是一些 Small Basic 编译器对第三方扩展库的规则,让你开发的扩展库能被 Small Basic 所识别和加载。1. 类型必须声明为静态的static2. 类型必须修饰为兼容 Small Basic 类型属性SmallBasicTy
2、peAttribute3. 属性必须要符合 Microsoft.SmallBasic.Library.Primitive 类型4. 所有方法的输入和输出参数都必须符合Microsoft.SmallBasic.Library.Primitive 类型5. 所有事件必须符合 Microsoft.SmallBasic.Library.SmallBasicCallback 类型当以上条件都满足的时候,你就可以编译你的程序集并把他放置到 SmallBasic程序的 Lib 目录下。例如,你的 Small Basic 如果安装在 C 盘,你开发的扩展库名称叫“myextensions”, 那么你就把 my
3、extensions.dll 放到“c:program filesmicrosoftsmall basiclib”文件夹中。此文由 Small Basic 中文站-快乐编程翻译。转载请注明出处:SmallB顺便说一下,你也可以开启 XML 说明文档,把 XML 说明文档文件一起放进 Lib目录下,他将会在你开发的时候,自动启用帮助信息,智能显示在帮助面板中。以下是一个 C#编写的扩展库实例,SmallB 也转换了一份 VB.Net 的供大家参考using System.Collections.Generic;using System.IO;using System.Reflection;usi
4、ng System.Runtime.Serialization.Formatters;using System.Runtime.Serialization.Formatters.Binary;using Microsoft.SmallBasic.Library;namespace MyExtensions/ / The Settings library consists of helpers that allow programs to / store and retrieve user settings./ SmallBasicTypepublic static class Settings
5、static Primitive _filePath = new Primitive();/ / Gets the file path for the settings file./ public static Primitive FilePathgetif (string.IsNullOrEmpty(_filePath)_filePath = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, “.settings“);return _filePath;/ / Gets the value for the setting id
6、entified by the specified name./ / “name“/ The Name of the setting./ / / The Value of the setting./ public static Primitive GetValue(Primitive name)if (System.IO.File.Exists(FilePath)using (Stream stream = System.IO.File.Open(FilePath, FileMode.Open)Dictionary contents = ReadContents(stream);if (con
7、tents.ContainsKey (name) return contentsname; return “;/ / Sets a value for a setting identified by the specified name./ / “name“/ The Name of the setting./ / “value“/ The Value of the setting./ public static void SetValue(Primitive name, Primitive value)Dictionary contents = null;if (System.IO.File
8、.Exists(FilePath)using (Stream stream = System.IO.File.Open(FilePath, FileMode.Open)contents = ReadContents(stream);elsecontents = new Dictionary();contentsname = value;using (Stream stream = System.IO.File.Open(FilePath, FileMode.Create)WriteContents(stream, contents);static Dictionary ReadContents
9、(Stream stream)BinaryFormatter formatter = new BinaryFormatter();formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;return (Dictionary)formatter.Deserialize(stream);static void WriteContents(Stream stream, Dictionary map)BinaryFormatter formatter = new BinaryFormatter();formatter.AssemblyForma
10、t = FormatterAssemblyStyle.Simple;formatter.Serialize(stream, map);VB.Net 源代码如下: 更多源代码来自: SmallBasic 中文站Imports System.Collections.GenericImports System.IOImports System.ReflectionImports System.Runtime.Serialization.FormattersImports System.Runtime.Serialization.Formatters.BinaryImports Microsoft.S
11、mallBasic.LibraryNamespace MyExtensions The Settings library consists of helpers that allow programs to store and retrieve user settings. _Public NotInheritable Class SettingsPrivate Sub New()End SubShared _filePath As New Primitive() Gets the file path for the settings file. Public Shared ReadOnly
12、Property FilePath() As PrimitiveGetIf String.IsNullOrEmpty(_filePath) Then_filePath = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, “.settings“)End IfReturn _filePathEnd GetEnd Property Gets the value for the setting identified by the specified name. The Name of the setting. The Value o
13、f the setting. Public Shared Function GetValue(name As Primitive) As PrimitiveIf System.IO.File.Exists(FilePath) ThenUsing stream As Stream = System.IO.File.Open(FilePath, FileMode.Open)Dim contents As Dictionary(Of String, String) = ReadContents(stream)If contents.ContainsKey(name) ThenReturn conte
14、nts(name)End IfEnd UsingEnd IfReturn “End Function Sets a value for a setting identified by the specified name. The Name of the setting. The Value of the setting. Public Shared Sub SetValue(name As Primitive, value As Primitive)Dim contents As Dictionary(Of String, String) = NothingIf System.IO.File
15、.Exists(FilePath) ThenUsing stream As Stream = System.IO.File.Open(FilePath, FileMode.Open)contents = ReadContents(stream)End UsingElsecontents = New Dictionary(Of String, String)()End Ifcontents(name) = valueUsing stream As Stream = System.IO.File.Open(FilePath, FileMode.Create)WriteContents(stream
16、, contents)End UsingEnd SubPrivate Shared Function ReadContents(stream As Stream) As Dictionary(Of String, String)Dim formatter As New BinaryFormatter()formatter.AssemblyFormat = FormatterAssemblyStyle.SimpleReturn DirectCast(formatter.Deserialize(stream), Dictionary(Of String, String)End FunctionPrivate Shared Sub WriteContents(stream As Stream, map As Dictionary(Of String, String)Dim formatter As New BinaryFormatter()formatter.AssemblyFormat = FormatterAssemblyStyle.Simpleformatter.Serialize(stream, map)End SubEnd ClassEnd Namespace