我不是 VB 开发人员,但我正在领导一个部分使用 VB 的大型项目。要求之一是实现插件架构以支持动态可扩展的应用程序核心。
我们的 VB 开发人员似乎认为可以将 BLL 存储在 DLL 中 - 将接口全部保留在原始核心应用程序中 - 在安装扩展之前使其无用。
显然,这不太理想。我想知道是否可以将整个子应用程序/组件保留在一个不同的 DLL 中,将其加载到核心平台中???
有任何想法吗?
这根本不是问题,看看我的示例代码。这是存在于单独 dll 中的抽象类:
public abstract class ServerTask
{
public int TaskId { get; set;}
/// <summary>
/// Gets description for ServerTask
/// </summary>
public abstract string Description { get; }
}
这是插件实现的代码:
public class SampleTask : GP.Solutions.WF.Entities.Tasks.ServerTask
{
public override string Description
{
get
{
return "Sample plugin";
}
}
}
最后来自加载插件的核心应用程序的代码:
/// <summary>
/// Loads Plugin from file
/// </summary>
/// <param name="fileName">Full path to file</param>
/// <param name="lockFile">Lock loaded file or not</param>
/// <returns></returns>
static Entities.Tasks.ServerTask LoadServerTask(string fileName, bool lockFile, int taskId)
{
Assembly assembly = null;
Entities.Tasks.ServerTask serverTask = null;
if (lockFile)
{
assembly = System.Reflection.Assembly.LoadFile(fileName);
}
else
{
byte[] data = Services.Common.ReadBinaryFile(fileName);
assembly = System.Reflection.Assembly.Load(data);
}
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
if (t.IsSubclassOf(typeof(Entities.Tasks.ServerTask)))
{
serverTask = (Entities.Tasks.ServerTask)Activator.CreateInstance(t);
serverTask.TaskId = taskId;
break;
}
}
if (serverTask == null)
{
throw new Exception("Unable to initialize to ServerTask type from library '" + fileName + "'!");
}
return serverTask;
}
如果您不熟悉 c#,只需使用 c# 到 vb.net 在线转换器。
快乐的编码和最好的问候!格雷戈尔·普里玛