You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.IO;
using Mesnac.PlugIn.View;
namespace Mesnac.Gui.PlugIn
{
public class PlugInService
{
/// <summary>
/// 扫描插件
/// </summary>
/// <param name="plugInPath">插件目录</param>
/// <returns>返回插件列表</returns>
public static List<PlugInDescriptor> ScanPlugIn(string plugInPath)
{
List<PlugInDescriptor> plugIns = new List<PlugInDescriptor>(); //保存插件列表
DirectoryInfo dir = new DirectoryInfo(plugInPath); //定义插件目录对象
FileInfo[] plugInFiles = dir.GetFiles("*.dll", SearchOption.TopDirectoryOnly); //扫描插件目录下的所有插件文件只扫描顶层目录下的dll文件
PlugInDescriptor plugIn = null; //定义插件变量
Assembly assembly = null;
foreach (FileInfo fi in plugInFiles)
{
plugIn = new PlugInDescriptor();
#region 解析插功能名称
assembly = Assembly.LoadFile(fi.FullName);
string functionName = String.Empty; //dll对应功能名称
IList<CustomAttributeData> lst = assembly.GetCustomAttributesData();
foreach (CustomAttributeData data in lst)
{
if (data.Constructor.DeclaringType.FullName == "System.Reflection.AssemblyDescriptionAttribute")
{
functionName = data.ConstructorArguments[0].Value as string;
break;
}
}
if (String.IsNullOrEmpty(functionName))
{
foreach (CustomAttributeData data in lst)
{
if (data.Constructor.DeclaringType.FullName == "System.Reflection.AssemblyTitleAttribute")
{
functionName = data.ConstructorArguments[0].Value as string;
break;
}
}
}
plugIn.FunctionName = functionName; //获取插件功能名称
#endregion
#region 解析视图窗口
List<IViewContent> viewContents = new List<IViewContent>();
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
Type[] result = t.FindInterfaces(IViewContentTypeFilter, null);
if (result != null && result.Length > 0)
{
IViewContent content = Activator.CreateInstance(t) as IViewContent;
viewContents.Add(content);
}
}
plugIn.ViewContents = viewContents; //加载窗口集合
#endregion
plugIns.Add(plugIn); //把插件添加到插件列表中
}
return plugIns;
}
/// <summary>
/// 视图窗口接口过滤
/// </summary>
/// <param name="type"></param>
/// <param name="target"></param>
/// <returns></returns>
public static bool IViewContentTypeFilter(Type type, object target)
{
if (type.FullName == "Mesnac.PlugIn.View.IViewContent")
{
return true;
}
return false;
}
}
}