从C#插件中的主应用程序访问数据 [英] Access data from main application within a plugin in C#

查看:123
本文介绍了从C#插件中的主应用程序访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种方法来使我的C#应用​​程序可扩展为其他开发人员:

创建简单的插件机制

它工作得很好,但只能在一个方向"上进行,这意味着编写新插件的开发人员可以在插件中定义方法和变量,并将这些方法和变量导入到我的应用程序中.

现在我的问题是:我如何从插件中的主应用程序访问现有数据(例如变量字符串测试")?

解决方案

有几种方法.

您可以创建一个Dictionary<string, object>,每个模块都可以访问以设置或获取数据.您应该编写一些逻辑来访问该词典.例如,一种T GetData<T>(string key)方法将位类型安全性引入其中.

您可以使用PRISM框架中的事件系统(例如EventAggregator)来启用模块之间的通信.

但是请注意,这会使您的模块彼此依赖.这就是为什么你应该处理例如如果无法从其他模块获得数据.

I am using this approach to make my C#-application extensible for other developers:

Creating a simple plugin mechanism

It works just fine but only in "one direction" which means that the developer who writes a new plugin can define methods and variables within the plugin and those will be imported to my application.

So my question is now: How can i access already existing data from my main application (e.g. a variable "string test") within the plugin?

解决方案

There are several ways.

You could create a Dictionary<string, object> where every module has access to set or get data. You should write some logic to access that dictionary. For example a T GetData<T>(string key) method to bring a bit type safety into it.

You could use an event system like EventAggregator in the PRISM framework to enable communication between your modules as well.

But be aware hat this will make your modules dependant from each other. That's why you should handle e.g. if data is not available from another module.

这篇关于从C#插件中的主应用程序访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆