LiangdunSdk.IPluginShareService 提供了插件之间共享功能。
服务介绍
使用本服务可以提供一些接口给其他插件使用,而不需要引入提供接口的dll。
类库调用
你需要导入LiangdunSdk.dll
接口介绍
/// 无参,无返回值
void DoMethod(string symbolicId, string className, string methodName);
/// 无参,有返回
object DoMethod(string symbolicId, string className, string methodName, Type reslutType);
/// 有参,无返回值
void DoMethod(string symbolicId, string className, string methodName, object[] paramesObj, Type[] paramesType);
/// 有参,有返回值方法
object DoMethod(string symbolicId, string className, string methodName, Type reslutType, object[] paramesObj, Type[] paramesType);
/// 修改类对象
bool UpdateObject(IBundle bundle, object classObj);
/// 添加类对象(严格遵守大小写)
bool AddObject(IBundle bundle, object classObj);
参数
///<param name="bundle">插件对象</param>
/// <param name="symbolicId">插件唯一ID</param>
/// <param name="className">类名</param>
/// <param name="methodName">方法名</param>
/// <param name="reslutType">返回值类型</param>
/// <param name="paramesObj">参数集</param>
/// <param name="paramesType">参数类型集</param>
/// <exception cref="Exception">找不到类对象或者方法</exception>
/// <returns></returns>
DoMethod(string symbolicId, string className, string methodName, Type reslutType, object[] paramesObj, Type[] paramesType);
使用
插件1:创建共享共享操作类,共享操作类必须存在无参构造函数
class TestClass
{
public string Text = "TestClass";
public void ADD(int a, int b)
{
MessageBox.Show(string.Format("A+B={0}", a + b));
}
public void SayText()
{
MessageBox.Show(string.Format("This Text is {0}", Text));
}
public int ReslutText()
{
return 1000;
}
public int AddRes(int a, int b)
{
return a + b;
}
}
插件1:将共享对象注册进sdk共享接口,("TestClass")类名参数必须唯一,如果已经存在将注册失败。共享对象必须存在无参构造函数
SdkFactoryService sdkService= context.GetFirstOrDefaultService<SdkFactoryService>();
if(sdkService!=null)
{
Sdk sdk = sdkService.GetSdk(context,null);
IPluginShareService pShareSer = sdk.GetPluginShareService();
//将共享对象注册进sdk共享接口
pShareSer.AddObject(bundle, new TestClass());//bundle:该插件的bundle对象
//修改共享对象
pShareSer.AddObject(bundle, new TestClass(string key1,string key2));
}
插件2:共享对象调用
IPluginShareService pShareSer;
public Form1()
{
InitializeComponent();
pShareSer = Activator.sdk.GetPluginShareService();
}
//无参,无返回
private void Btn_1_Click(object sender, EventArgs e)
{
pShareSer.DoMethod("PluginShareServiceTestInput", "TestClass", "SayText");
}
//无参,有返回值
private void Btn_2_Click(object sender, EventArgs e)
{
Type resultType = typeof(string);
object resObj = pShareSer.DoMethod("PluginShareServiceTestInput", "TestClass", "ReslutText", resultType);
this.textBox1.Text = "";
this.textBox1.Text = resObj.ToString();
}
//有参,无返回
private void Btn3_Click(object sender, EventArgs e)
{
object[] objs = new object[] { 1234, 2345 };
Type[] objTypes = new Type[] { typeof(int), typeof(int) };
pShareSer.DoMethod("PluginShareServiceTestInput", "TestClass", "ADD", typeof(void), objs, objTypes);
}
//有参,有返回
private void Btn4_Click(object sender, EventArgs e)
{
object[] objs = new object[] { 1234, 2345 };
Type[] objTypes = new Type[] { typeof(int), typeof(int) };
Type resultType = typeof(int);
object resObj = pShareSer.DoMethod("PluginShareServiceTestInput", "TestClass", "AddRes", resultType, objs, objTypes);
this.textBox1.Text = "";
this.textBox1.Text = resObj.ToString();
}
异常信息
DoMethod(string symbolicId,string className, string methodName)
找不到对应className 参数会抛出 “System.Exception("找不到该方法");”
找不到对应methodName 参数会抛出 “System.Exception("找不到类对象");”
网友评论