Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a standalone application that exports some data from a database. In this program I have a custom object, ExportParams, that keeps the database information, server, database name etc, that can come from the user interface, a config file or from the command prompt. I now need to change this standalone application into a plugin. The user interface I have converted into a plugin and can run without issue. The issue I am having is how do I access this custom object from the main form so I can send in the parameters from the command prompt or config file into the plugin?
Posted
Comments
BillWoodruff 28-Feb-14 8:54am    
You have set a reference to the dll for your project ? You have any required "using" statements to expose the functionality you wish to access ? What are you using for plug-in "architecture:" WCF ? ... or ?

I suggest you give more details about the "custom object."
shwags 28-Feb-14 9:27am    
I am using Reflection. I have reference and using in the plugin to the main but not the other way.

The object is a handful of strings and a couple of lists.

1 solution

The common mistake in using reflection is extracting some types and type members by names. This is not supportable, because those names become "magic words": any refactorization can lead to change of these names; and the mismatch between names declared in code and in data cannot be detected as a compilation error.

At the same time, right approach is 100% reliable and will withstand any renaming. Here is what you can do:

Define some plug-in interface. It can be defined in some assembly referenced by both host application and plug-in assemblies. A bit more tricky way is to define it right in the host application assembly. It always can be done, because application assembly can be referenced by a plug-in assembly exactly as it was a library; it would not create any circular dependencies, because the application assembly does not depend on plug-ins, only the runtime depends on them.

Now, when you load the plug-in assembly, the host application can scan its types and determine the one(s) implementing the plug-in interface(s). This can be done using this function:
http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom%28v=vs.110%29.aspx[^].

I do even better. I define a special assembly-level attribute (System.AttributeTargets.Assembly; please see http://msdn.microsoft.com/en-us/library/system.attributeusageattribute%28v=vs.110%29.aspx[^], http://msdn.microsoft.com/en-us/library/system.attributetargets%28v=vs.110%29.aspx[^]) used just to claim which type(s) should be checked as implementing the plug-in interfaces. Then the search in the list of all assembly types is excluded.

The bigger problems appear when the set of loaded plug-ins should vary during runtine, that is, when you need to unload some assemblies. For important reliability reasons, .NET does not allow that. So, you would need to load plug-ins in separate Application Domains which can be fully unloaded, but it requires working with IPC, to reach through the boundaries between Application Domains. You can find the detail in my past answers:
Gathering types from assemblies by it's string representation[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL's: How to keep them managable?[^],
Dynamically Load User Controls[^],
code generating using CodeDom[^],
AppDomain refuses to load an assembly[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900