Small Basic中文站-快乐编程

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索

Small Basic第三方扩展库开发指南

2010-6-16 23:37| 发布者: shiny| 查看: 9435| 评论: 0

摘要: 本文指导如何开发Small Basic第三方扩展库,包括扩展库开发的规则、实例。

此文由Small Basic中文站-快乐编程翻译。

Small Basic编译器支持第三方类库作为插件的方式加载,这样就可以增加自己感兴趣的扩展库了。

此文来自SmallBasic.Cn  (Small Basic中文站-快乐编程)   转载请注明出处

开发的类库必须基于.Net语言并且编译为.Net程序集。以下是一些Small Basic编译器对第三方扩展库的规则,

让你开发的扩展库能被Small Basic所识别和加载。

  1. 类型必须声明为静态的[static]
  2. 类型必须修饰为兼容Small Basic类型属性[SmallBasicTypeAttribute]
  3. 属性必须要符合Microsoft.SmallBasic.Library.Primitive类型
  4. 所有方法的输入和输出参数都必须符合Microsoft.SmallBasic.Library.Primitive 类型
  5. 所有事件必须符合 Microsoft.SmallBasic.Library.SmallBasicCallback 类型

当以上条件都满足的时候,你就可以编译你的程序集并把他放置到SmallBasic程序的Lib目录下。

例如,你的Small Basic 如果安装在C盘,你开发的扩展库名称叫“myextensions”,

那么你就把myextensions.dll放到“c:\program files\microsoft\small basic\lib”文件夹中。

此文由Small Basic中文站-快乐编程翻译。转载请注明出处:SmallBasic.cn

顺便说一下,你也可以开启XML说明文档,把XML说明文档文件一起放进Lib目录下,

他将会在你开发的时候,自动启用帮助信息,智能显示在帮助面板中。

以下是一个C#编写的扩展库实例,SmallBasic.cn也转换了一份 VB.Net的供大家参考

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
{
    ///
    /// The Settings library consists of helpers that allow programs to
    /// store and retrieve user settings.
    ///

    [SmallBasicType]
    public static class Settings
    {
        static Primitive _filePath = new Primitive();
 
        ///
        /// Gets the file path for the settings file.
        ///

        public static Primitive FilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_filePath))
                {
                    _filePath = Path.ChangeExtension(
                                    Assembly.GetEntryAssembly().Location,
                                    ".settings");
                }
 
                return _filePath;
            }
        }
 
        ///
        /// Gets the value for the setting identified 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<string, string> contents = ReadContents(stream);
                    if (contents.ContainsKey  (name)) { return contents[name]; }
                }
            }
 
            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<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);
        }
    }
}

 

VB.Net源代码如下: 更多源代码来自: SmallBasic中文站

Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Binary
Imports Microsoft.SmallBasic.Library

Namespace MyExtensions
	''' 
	''' The Settings library consists of helpers that allow programs to 
	''' store and retrieve user settings.
	''' 
	 _
	Public NotInheritable Class Settings
		Private Sub New()
		End Sub
		Shared _filePath As New Primitive()

		''' 
		''' Gets the file path for the settings file.
		''' 
		Public Shared ReadOnly Property FilePath() As Primitive
			Get
				If String.IsNullOrEmpty(_filePath) Then
					_filePath = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".settings")
				End If

				Return _filePath
			End Get
		End Property

		''' 
		''' Gets the value for the setting identified by the specified name.
		''' 
		''' 
		''' The Name of the setting.
		''' 
		''' 
		''' The Value of the setting.
		''' 
		Public Shared Function GetValue(name As Primitive) As Primitive
			If System.IO.File.Exists(FilePath) Then
				Using stream As Stream = System.IO.File.Open(FilePath, FileMode.Open)
					Dim contents As Dictionary(Of String, String) = ReadContents(stream)
					If contents.ContainsKey(name) Then
						Return contents(name)
					End If
				End Using
			End If

			Return ""
		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) = Nothing
			If System.IO.File.Exists(FilePath) Then
				Using stream As Stream = System.IO.File.Open(FilePath, FileMode.Open)
					contents = ReadContents(stream)
				End Using
			Else
				contents = New Dictionary(Of String, String)()
			End If

			contents(name) = value
			Using stream As Stream = System.IO.File.Open(FilePath, FileMode.Create)
				WriteContents(stream, contents)
			End Using
		End Sub

		Private Shared Function ReadContents(stream As Stream) As Dictionary(Of String, String)
			Dim formatter As New BinaryFormatter()
			formatter.AssemblyFormat = FormatterAssemblyStyle.Simple
			Return DirectCast(formatter.Deserialize(stream), Dictionary(Of String, String))
		End Function

		Private Shared Sub WriteContents(stream As Stream, map As Dictionary(Of String, String))
			Dim formatter As New BinaryFormatter()
			formatter.AssemblyFormat = FormatterAssemblyStyle.Simple
			formatter.Serialize(stream, map)
		End Sub
	End Class
End Namespace
 

鲜花

握手
1

雷人
2

路过
1

鸡蛋

刚表态过的朋友 (4 人)

最新评论

QQ|手机版|Small Basic中文站-快乐编程 ( 闽ICP备09051788号 |

GMT+8, 2017-12-13 09:35 , Processed in 0.080953 second(s), 11 queries , File On.

Powered by Discuz! X3.3

返回顶部