您的位置:首页 > 移动开发

Application应用框架思考(三) 之[插件机制]

2009-04-17 22:06 281 查看
 
Application应用框架思考(三)  之[插件机制]

/// <summary>
/// 单例类
/// //为子功能提供主框架信息接口 类
/// </summary>
public class ApplicationProvider : IApplicationProvider, IConnectable
{
public  ApplicationProvider()
{
}

private static ApplicationProvider instance = new ApplicationProvider();

public IApplication Application
{
get
{
return instance.m_Application;
}
}

#region IConnectable 成员

private IApplication m_Application;
public void Connect(IApplication Application)
{
instance.m_Application = Application;
}

#endregion

}

/// <summary>
/// //为子功能提供主框架信息接口
/// </summary>
public interface IApplicationProvider
{
}
/// <summary>
/// 能连接到Application宿主程序
/// </summary>
public interface IConnectable
{
void Connect(IApplication Application);
}

/// <summary>
/// 标志插件IPlugIn接口
/// </summary>
public interface IPlugIn : IApplicationProvider
{
/// <summary>
/// 插件汉化名称
/// </summary>
string PlugInChineseName { get; }
}

/// <summary>
/// 默认适配器类
/// </summary>
public abstract class PlugInClass : IPlugIn
{

#region IPlugIn 成员

public virtual string PlugInChineseName
{
get { return ""; }
}

#endregion

#region IApplicationProvider 成员

private IApplication m_Application = null;
public virtual void Connect(IApplication Application)
{
m_Application = Application;
}

#endregion

}

public class ExecuterLoadDLLToMemory
{
IPlugInEx hook = null;
public ExecuterLoadDLLToMemory(object phook)
{
try
{
hook = phook as IPlugInEx;
}
catch
{
}
}
/// <summary>
/// 使用属性 Assembly/Expression/
/// </summary>
/// <param name="tdcmd"></param>
public void LoadDLLToMemory(ref ICommandEx tdcmd)
{
try
{

if (hook.Assembly == null || hook.Assembly.Trim().Length <= 0 || hook.Expression.Trim().Length<=0)
{
return;
}
string tmpPath = GISApplication.Instance.ApplicationStartupPath + "//" + hook.Assembly;
if (System.IO.File.Exists(tmpPath) == false)
{
return;
}
object objHdl = null;
Type type = null;
string AppPath = GISApplication.Instance.ApplicationStartupPath;
if (GISApplication.Instance.MainAppEx.ALL_DllInstanceSet.ContainsKey(hook.Expression) == false)
{
string Dllfilename = AppPath + "//" + hook.Assembly;

System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(Dllfilename);

type = assembly.GetType(hook.Expression);
//构造函数
objHdl = (object)Activator.CreateInstance(type);
//objHdl = (object)Activator.CreateInstance(type, args);

GISApplication.Instance.MainAppEx.ALL_DllInstanceSet.addDllInstance(hook.Expression, objHdl, type);
}
else
{
//取出实例化值
ExtendPropertySet meps = GISApplication.Instance.MainAppEx.ALL_DllInstanceSet.getValue(hook.Expression);
objHdl = meps.DllInstance;
type = meps.type;
meps = null;
}

//调用执行类中的方法
//MM_GetInvokeMethod(type, method, hook.Method, objHdl);

tdcmd = objHdl as ICommandEx;

objHdl = null;
type = null;

}
catch (Exception e)
{
tdcmd = null;
MessageBox.Show(hook.Assembly + Environment.NewLine + hook.Expression + Environment.NewLine +

Environment.NewLine + e.ToString());
}
}

//获取和调用方法
private void MM_GetInvokeMethod(Type type, MethodInfo method, string methodName, object objHdl)
{
method = type.GetMethod(methodName);
ParameterInfo[] myParameters = method.GetParameters();
if (myParameters.Length <= 0)
{
//(不带参数的方法)
method.Invoke(objHdl, new object[] { });
}
else
{
//(带参数的方法)
string tt = "sfsdfsdf";
method.Invoke(objHdl, new object[] { tt });
}
}

//获取和调用属性
private void MM_GetInvokeProperty(Type type, string methodName, object objHdl, object oValue)
{
PropertyInfo property = null;
property = type.GetProperty(methodName);
property.SetValue(objHdl, oValue, null);
}
}
/// <summary>
/// //为子功能提供主框架信息接口  扩展
/// </summary>
public interface IApplicationProviderEx : IApplicationProvider,IConnectableEx
{
}

public interface IConnectableEx
{
void Connect(IGISApplication Application);
}
public interface IPlugInEx : IPlugIn,IApplicationProviderEx
{
/// <summary>
/// Parent父级对象
/// </summary>
object Parent { get; set; }
/// <summary>
/// GUID
/// </summary>
string objGuid { get; set; }

bool objChecked { get; set; }

bool objVisible { get; set; }

bool objEnabled { get; set; }

bool IsExecute { get; set; }
//属性
string ShortCutKeys { get; set; }                 //快捷键
string ToolBarItemType { get; set; }              //何种类型按钮
string ToolStripItemDisplayStyle { get; set; }    //显示状态
string Expression { get; set; }                   //命名空间.类名
string Assembly { get; set; }                     //dll文件名

/// <summary>
/// 执行方法
/// </summary>
void Execute();

/// <summary>
/// 初始化操作方法
/// </summary>
void init();

ExecuterLoadDLLToMemory LoadDLLToMemory { get; }

}

/// <summary>
/// 基础通用插件类
/// </summary>
public class PlugInExClass : ICommandHolder, IPlugInEx,ICallExecuteAble
{
public PlugInExClass()
{
}

#region ICommandHolder 成员
private ICommandEx pCommand = null;
public ICommandEx getCommand()
{
return pCommand;
}

public void setCommand(ICommandEx cmd)
{
pCommand = cmd;
}

public void setParentIPlugIn(IPlugInEx ParentIPlugIn)
{
}

#endregion

#region IPlugInEx 成员
private object pParent = null;
public new object Parent
{
get
{
return this.pParent;
}
set
{
this.pParent = value;
}
}

private string pobjGuid = "";
public string objGuid
{
get
{
return this.pobjGuid;
}
set
{
this.pobjGuid = value;
}
}
//------------------------------
public bool objChecked
{
get
{
return false;
}
set
{

}
}

public bool objVisible
{
get
{
return false;
}
set
{

}
}

private bool Enabled = false;
public bool objEnabled
{
get
{
return this.Enabled;
}
set
{
this.Enabled = value;
}
}
//-----------------------------
private bool pIsExecute = false;
public bool IsExecute
{
get
{
return this.pIsExecute;
}
set
{
this.pIsExecute = value;
}
}
private string pShortCutKeys = "";
public string ShortCutKeys
{
get
{
return this.pShortCutKeys;
}
set
{
this.pShortCutKeys = value;
}
}
private string pToolBarItemType = "";
public string ToolBarItemType
{
get
{
return this.pToolBarItemType;
}
set
{
this.pToolBarItemType = value;
}
}
private string pToolStripItemDisplayStyle = "";
public string ToolStripItemDisplayStyle
{
get
{
return this.pToolStripItemDisplayStyle;
}
set
{
this.pToolStripItemDisplayStyle = value;
}
}
private string pExpression = "";
public string Expression
{
get
{
return this.pExpression;
}
set
{
this.pExpression = value;
}
}
private string pAssembly = "";
public string Assembly
{
get
{
return this.pAssembly;
}
set
{
this.pAssembly = value;
}
}

public void Execute()
{
ICommandEx cmd = this.getCommand();
if (cmd != null)
{
if (cmd is ITool)
{
GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentTool = cmd as ITool;
}
else
{
GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentTool = null;
}
cmd.OnCreate(GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentHook);
cmd.OnClick();
}
else
{
//初始化一个功能
this.LoadDLLToMemory.LoadDLLToMemory(ref cmd);
if (cmd != null)
{
this.setCommand(cmd);
if (cmd is ITool)
{
GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentTool = cmd as ITool;
}
else
{
GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentTool = null;
}
cmd.OnCreate(GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentHook);
cmd.OnClick();
}
}
}
/// <summary>
/// 初始化ITDCommand实例
/// </summary>
public void init()
{
ICommandEx cmd = this.getCommand();
if (cmd == null)
{
//初始化一个功能
this.LoadDLLToMemory.LoadDLLToMemory(ref cmd);
if (cmd != null)
{
if (GISApplication.Instance.MainAppEx.CurrentArcGISControl != null)
{
cmd.OnCreate(GISApplication.Instance.MainAppEx.CurrentArcGISControl.CurrentHook);
}
this.setCommand(cmd);
}
}
}

private ExecuterLoadDLLToMemory pLoadDLLToMemory = null;
public ExecuterLoadDLLToMemory LoadDLLToMemory
{
get
{
if (this.pLoadDLLToMemory == null)
{
this.pLoadDLLToMemory = new ExecuterLoadDLLToMemory(this);
}
return pLoadDLLToMemory;
}
}

#endregion

#region IPlugIn 成员

public string PlugInChineseName
{
get { return "default PlugInExClass"; }
}

#endregion

#region IApplicationProvider 成员

private IApplication m_Application = null;
public void Connect(IApplication Application)
{
m_Application = Application;
}

#endregion

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息