I've created a simple Managed Extensibility Framework calculator system that uses different dll file's containing method like subtraction, addition.
//API.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace API
{
public interface IPlugin
{
string DoAction(int x,int y);
string Name { get; }
}
}
//ClientApp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace ClientApp
{
class ClientConsole
{
[ImportMany(typeof(API.IPlugin))]
private List<API.IPlugin> allPlugins;
public ClientConsole()
{
DirectoryCatalog catalog = new DirectoryCatalog(Environment.CurrentDirectory);
CompositionContainer worker = new CompositionContainer(catalog);
worker.ComposeParts(this);
}
public void DoActions()
{
foreach (var item in allPlugins)
{
Console.WriteLine(item.Name);
Console.WriteLine( item.DoAction(5,2));
}
}
}
class Program
{
static void Main(string[] args)
{
new ClientConsole().DoActions();
Console.ReadLine();
}
}
}
//MailPlugin.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
namespace MainPlugin
{
[Export(typeof(API.IPlugin))]
public class Class1:API.IPlugin
{
public string DoAction(int x,int y)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
x = x + y;
return Convert.ToString(x);
}
public string Name
{
get { return "Addition"; }
}
}
}
//Plugin2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
namespace Plugin2
{
[Export(typeof(API.IPlugin))]
public class Class1 : API.IPlugin
{
public string DoAction(int x, int y)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
x = x - y;
return Convert.ToString(x);
}
public string Name
{
get { return "Subtraction"; }
}
}
}
Output:
addition
7
subtraction
3
something i want to do is to be able to choose which function only to use, like, out of the four available function add,subtract,multiply,divide, I will only use add and divide, each of this function are seperated in their own dll files.
what I was originally planning is to access the list of plugins from "List<API.IPlugin> allPlugins" and change the
" foreach (var item in allPlugins)
{
Console.WriteLine(item.Name);
Console.WriteLine( item.DoAction(5,2));
}"
into something where the program only runs the plugins I only choose and not loop and run everything. But I have no Idea how to access the list of plugins. Can any one enlighten me. I am quite new to this. Thank you
Choosing the dll's to be use in Managed Extensibility Framework.
Started by kenenth88, Nov 15 2011 11:03 PM
No replies to this topic
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









