How to construct a C# plug-in framework? - c#

I would like to implement a Plug-In framework for a C# application; such that the application doesn’t need to know of all of the available .DLL’s at time of compilation but can then be dynamically linked at run time. I expect that I will need to setup some expected parameters to pass information in and out of Plug-In’s but I’m not really sure of what else is needed or how to go about this in C#. Could you point me to a pattern for this implementation or a framework that I can implement to achieve this (if API’s - Open Source license required)?

The Managed Extensibility Framework (MEF) is definitely something you want to take a look at. It's developed by Microsoft, and the stable release version is included in .NET 4.0 and later.

You should have a look at Mono.Addins library. It's open source, easy to use, and really powerful (I think it's even used by MonoDevelop itself).

Related

How to call a c# dll from java program

I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.

Approach to share code between Compact Framework, Silverlight, WP7 and full .NET runtime

I am creating libraries that I will use across Compact Framework, Silverlight, WP7 and the full .NET runtime. I am aware that the question around sharing between Compact Framework and full .NET, or between Silverlight and full .NET has been asked many times and I have been reading all the answers around that, however this situation is further complicated because I have to use VS2010 for Silverlight/WP7 and VS2008 for Compact Framework.
I therefore need to use multiple solutions along with multiple projects for this.
Is there a suggested "best-practice" approach for managing this, I am aware that I can create the multiple solutions/projects using add-file-as-link functionality to maintain the project, however this becomes a manual process open to error which I'd like to avoid.
Has anyone had any experience with automating the build of seperate frameworks, for example creating and maintaining a single .NET project, but having a custom build action which tweaks the solution and project files automatically and building several output assemblies for the different required frameworks. I am aware there is an added complications with making sure the correct references are generated.
Is there an existing framework that achieves this, I've had a search around but can't see anything. Alternatively is there an appetite for the creation of such a framework?
This session from PDC2010 might help:
http://blogs.microsoft.co.il/blogs/arik/archive/2010/10/31/pdc-2010-3-screen-coding-sharing-code-between-windows-phone-silverlight-and-net.aspx
Microsoft have announced that the Portable Library Project will be available H1 this year - so it should be available real soon - maybe at Mix?
Until then, the best advice seems to be to create a SL 3 class library project for most of your sharing - WP7, WPF, SL-Web, .Net desktop - but you'll then still need to do something special for WM6 (but for WM6 I still need to do lots of special things anyway - like it still insists on using VS2008!)

Building An App With Plug-in Support

I'm starting a new project which would greatly benefit from program add-ons. The program in its most basic form reads data from a serial port and parses it into database records. Examples of add-ons that could be written would be an auto-archive add-on, an add-on to filter records, etc. I'm writing both the program and the add-ons, but some customers need custom solutions, so instead of branching off and making a completely separate program, add-ons would be great. The simplest add-on would probably be a form who's constructor takes an object reference, manipulates the object in some way, then closes.
Unfortunately, I have absolutely no idea where to start coding, and almost as little idea where to search. Everything I search for turns up browser add-ons. From what I have gathered, I need to look into dynamic loading DLLs. Besides that, I'm clueless. Does anyone have any good resources or examples I that they know of?
I'm happy to provide more details, but this project is in its inception, so I don't have a ton of specific details (specifics kind of defeats the point of add-ons, too.)
You should seriously consider using the Managed Extensibility Framework (MEF) to handle your plugin architecture. It requires thinking about things a little differently, but it is well worth the mind-stretch.
This is a simple example to illustrate the basic technique.
codeproject.com - Plugin Architecture using C#
This article demonstrates to you how
to incorporate ... as a
plugin for another application or use
it as a standalone application.
in .NET 4 you now have the Managed Extensibility Framework (MEF) to do much of the plumbing.
In .NET 3.5 you had the System.AddIn but it was deemed by many to be far too complex.
codeproject.com - AddIn Enabled Applications with System.AddIn
AddIns (sometimes called Plugins) are
seperately compiled components that an
application can locate, load and make
use of at runtime (dynamically). An
application that has been designed to
use AddIns can be enhanced (by
developing more AddIns) without the
need for the orginal application to be
modified or recompiled and tested
You really need to look at Managed Extensibility Framework (MEF). This is specifically designed to help support add-ons and other extensibility.
A very basic description (basically, your plugins must implement a special interface):
http://martinfowler.com/eaaCatalog/plugin.html
Much better article, in C#:
http://www.drdobbs.com/184403942;jsessionid=TVLM2PGYFZZB1QE1GHPCKHWATMY32JVN
I think Reflection will play a major role.
I expirimented with an app that had a plugin folder. A filesystem watcher would watch the folder, and when a new DLL was placed in it, it would use reflection to determine which types of plugins it included, loaded them, and added them to the list of available classes, etc.
Try using the term 'add-in' or 'plug-in' for your research instead of 'add-on'. That should help some.
If you're using .Net 4, there's an add-in namespace in the framework that will get you partway there.
Writing plug-in support for an app is no simple task. You'll have to maintain pretty strict separation-of-concerns across your interfaces, you'll need to provide an interop library that defines ALL of the supported plug-in types, and you'll want to do some research into dependency injection & inversion of control, in addition to the previously-suggested reflection research.
It sounds like you might have a busy weekend doing research.

Why C# is an open standard but .NET is not?

Why C# is an open standard but .NET is not? What is the point in this? Why Microsoft decide to open only some part of their .NET?
Various parts of the .NET runtime are indeed standardised by ECMA just like C# - CIL, the CLI, the CLS.
.NET is the runtime and C# is the language. C# can be compiled and run on other runtimes, such as Mono. I am actually not aware of any other runtimes besides Mono, but since the spec for C# is open, you could read it and make your own runtime. ;)
C#, like Java, C, C++, etc. is just a language definition. In and of itself, it does nothing. It defines the means by which a user can define a program or procedure and interface with external libraries.
The .NET framework, on the other hand, is not a language. It's a class library and development framework.
Actually, there is an open standard (ECMA 335 for the runtime api instead of ECMA 334 for the language).
Going beyond this, the source code for Microsoft's implementation of .Net is available and there are multiple separate implementations (the most prominent of which by far is mono).
There is some additional concern about patent encumbrance. However, Microsoft has also issued a legally binding and irrevocable community promise on the .Net platform that covers both specifications (a lot of people miss the legally binding part).
I assume you mean the framework. I guess they want to maintain control over the library implementation on Windows. There is nothing stopping someone from implementing a call-compatible version of all or part of the framework based on their own source as was done by Mono.

Example of c# based rule language?

Can you provide a good example of rule definition language written in C#.
Java guys have JESS, is there anything good for C#?
This page shows some examples of open-source rules engines in C#: http://csharp-source.net/open-source/rule-engines
You can use the forward chaining inference engine that is part of Windows Workflow Foundation (.NET 3.5 and higher) The best part is that this has a free runtime licensing.
You can use the Rule Manager from Acumen Business and install the Windows Workflow Foundation adapter. Once installed, export the rules as WFRules (*.rules).
A visual studio .NET solution will be generated that shows how the rule engine can be invoked standalone (no workflow is necessary)
See also http://bizknowledge.blogspot.com/search/label/Windows%20Workflow%20Foundation
Try http://rulesengine.codeplex.com
It has a fluent-interface wrapper for creating rules.
It's lightweight and simple to use.
You could use Windows Workflow Foundation's (WF) workflow engine with C#. I'd started a small and simple project using WF as the workflow engine, it's actually quite straightforward to use. Check out the first part entry I've been developing on this here.
What is interesting about WF is that you don't have to use the whole thing if you want to - if you only want to write some custom rules against some entities or objects, you can - quite ingenious! Also, it's a lot less to take on board than BizTalk's BRE (and no licensing cost).
You need to add a reference to the following .Net assemblies, available in the .Net Framework v3.0 and onwards:
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
Check out the article for more info.
There is the Microsoft Business Rules Engine: http://msdn.microsoft.com/en-us/library/aa561216.aspx. Not sure if it can only be used inside Biztalk - it does says it is a .Net Class Library.
Microsoft Business Rule Engine(BRE) is quite nice. But(and that's a big BUT) you'll need a BizTalk Server license to use it.
Take a look at Jetfire on codeplex. It supports forward chaining 'rules'.

Categories

Resources