Anton Shepelev:
I am working on a plugin host whose plugins are
.NET assemblies implementing a certain inter-
face. It would be beautiful if I could give the
plugins a custom extension (say, .plu) to make
them visibly different from ordinary (non-plug-
in) library assemblies which are not part of the
host and which those plugins may use. Do you
know of a way to do it in Visual Studio?
P.S.: I hope that my question is not entirely
off-topic.
With MEF it is relative simple:
C:\Work\plugin>type Common.cs
namespace E
{
public interface IHelloWorld
{
void Say();
}
}
C:\Work\plugin>csc /t:library Common.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\plugin>type A.cs
using System;
using System.ComponentModel.Composition;
namespace E
{
[Export(typeof(IHelloWorld))]
public class A : IHelloWorld
{
public void Say()
{
Console.WriteLine("Hello world from A");
}
}
}
C:\Work\plugin>csc /t:library /r:Common.dll
/r:System.ComponentModel.Composition.dll /out:A.plu A.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\plugin>type B.cs
using System;
using System.ComponentModel.Composition;
namespace E
{
[Export(typeof(IHelloWorld))]
public class B : IHelloWorld
{
public void Say()
{
Console.WriteLine("Hello world from B");
}
}
}
C:\Work\plugin>csc /t:library /r:Common.dll
/r:System.ComponentModel.Composition.dll /out:B.plu B.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\plugin>type Test.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace E
{
public class Test
{
[ImportMany]
private List<IHelloWorld> plugins = null;
private void DoAll()
{
foreach(IHelloWorld plugin in plugins)
{
plugin.Say();
}
}
public static void Main(string[] args)
{
Test o = new Test();
AggregateCatalog cat = new AggregateCatalog();
cat.Catalogs.Add(new DirectoryCatalog(@"C:\work\plugin", "*.plu"));
CompositionContainer container = new CompositionContainer(cat);
container.SatisfyImportsOnce(o);
o.DoAll();
}
}
}
C:\Work\plugin>csc /t:exe /r:Common.dll
/r:System.ComponentModel.Composition.dll
Test.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\plugin>Test
Hello world from A
Hello world from B