Компилятор Small Basic разрешает использовать внешние подключаемые библиотеки, что позволяет получить дополнительные возможности при разработке. Эти библиотеки могут быть собраны с помощью любого .NET-ориентированного языка программирования. Существует несколько правил при разработке библиотеки, выполнив которые, Small Basic определит вашу библиотеку как еще один объект (тип).
- Тип должен быть объявлен статическим.
- Тип должен быть обозначен как SmallBasicTypeAttribute
- Свойства должны иметь тип Microsoft.SmallBasic.Library.Primitive
- Все методы должны иметь тип Microsoft.SmallBasic.Library.Primitive
- Все события должны иметь тип Microsoft.SmallBasic.Library.SmallBasicCallback
Когда эти условия соблюдены, вы можете скомпилировать вашу библиотеку и положить ее в папку «lib», которая должна располагаться в установочной папке Small Basic. Например, если Small Basic был установлен на диск «C:» и ваша библиотека называется «myextensions», то вы должны поместить myextensions.dll в папку «c:\program files\microsoft\small basic\lib».
Ниже приведен образец расширения, написанный на C#. Пример добавляет объект Settings, с помощью которого можно хранить и восстанавливать пары «имя - значение» для конкретной программы.
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.SmallBasic.Library;
namespace MyExtensions
{
/// <summary>
/// The Settings library consists of helpers that allow programs to
/// store and retrieve user settings.
/// </summary>
[SmallBasicType]
public static class Settings
{
static Primitive _filePath = new Primitive();
/// <summary>
/// Gets the file path for the settings file.
/// </summary>
public static Primitive FilePath
{
get
{
if (string.IsNullOrEmpty(_filePath))
{
_filePath = Path.ChangeExtension(
Assembly.GetEntryAssembly().Location,
".settings");
}
return _filePath;
}
}
/// <summary>
/// Gets the value for the setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <returns>
/// The Value of the setting.
/// </returns>
public static Primitive GetValue(Primitive name)
{
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
Dictionary<string, string> contents = ReadContents(stream);
if (contents.ContainsKey (name)) { return contents[name]; }
}
}
return "";
}
/// <summary>
/// Sets a value for a setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <param name="value">
/// The Value of the setting.
/// </param>
public static void SetValue(Primitive name, Primitive value)
{
Dictionary<string, string> contents = null;
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
contents = ReadContents(stream);
}
}
else
{
contents = new Dictionary<string, string>();
}
contents[name] = value;
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Create))
{
WriteContents(stream, contents);
}
}
static Dictionary<string, string> ReadContents(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
return (Dictionary<string, string>)formatter.Deserialize(stream);
}
static void WriteContents(Stream stream, Dictionary<string, string> map)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.Serialize(stream, map);
}
}
}
* This source code was highlighted with Source Code Highlighter.
Обсудить материал можно на форуме: http://forum.smallbasic.ru/index.php?showtopic=18
Источник: http://blogs.msdn.com/smallbasic/archive/2008/10/27/extending-small-basic.aspx.