Plugin-dll compiled witd CSharpCodeProvider

Sep 9, 2010 at 5:53 PM

My problem ( beside my bad english ):
I have an app with plugins. I developed the structure with MEF. At start, the plugins are loaded. All works fine when i compile my plugins with vs2010.
I use .Net 4.0 and vs2010 professional.Below is a short description of my project(s). It works. It work great.MEF is great.
Now i have decided that users should write there own code ( as part of my method free_programmable(). I generate e file and compile it with the CSharpCodeProvider.
First i have had some problems with references and CompilerVersion but i solved it. CompilerVersion is v4.0 and the Referencelist is set in the CompilerParameters ( System.ComponentModel.Composition.dll is of course in the list )
GenerateExecutable is false, GenerateInMemory is false,CompilerOptions is "/lib:Library" because most of my dlls are in this directory.
With compiler.CompileAssemblyFromFile(cparams,"myName"); i generate my dll. No Errors occure.
I copy this dll in my plugin-directory and start my app but this dll do not work like the others ( the file is the same as in my vs2010 project. Same usings, same references )
I generate 3 dlls. I start my app, i see 3 plugins but it seems, that only the first plugin is realy loaded. In the description below you can see, that i store the
name of each plugin in a foreach-loop. This loop iterated 3 times ( for my 3 plugins ) but the name ( and later the methos free_programmable() is always that of
the first dlls. It seems that the first dll will be loaded 3 times. When i compile the same file in vs2010, i see the correct name and the correct method will be called.
I have mixed vs-compiled plugind and CSharpCodeProvider plugins. The vs-plugins are ok, instead of the self-compiled plugins always the first loaded appears.
And no Errors occure. I thinks, i made something wrong.But my brain is to small to solve this problem ( i am relative new in csharp and .net development but have 20 years
experience in Delphi and Pascal) 

namespace IVkn.Contract
{
  //my interface
  public interface IVkn
  {
    //....
    void init( /* some vars */)
    void free_programmable()
  }

  //a base-class
  public class PluginVkn : IVkn
  {
  //-...
    public void init(....){...}
    public virtual void free_programmable(){}
  }

   // my export attribs
  [MetadataAttribute]
  [AttributeUsage(AttributeTargets.Class)]
  public class VknAttribut:ExportAttribute
  {
    public VknAttribute(string name ):base(typeof(IVkn))
    {
      Name = name
    }
    public string Name { get; private set;}
  }
}

in a seconde project i extend my base-class a little bit
namespace Vkn
{
  public class Vkn : IVkn.Contract.PluginVkn
  {
    public MyUserControl uc = null;
    public void aMethod1(){..} // ect.

  }
}

 
my plugins as vs2010 project1 or as self-compiled
namespace VknPlugin
{
  using Vkn;
  ....

  [VknAttribute("StandardVkn")]
  public class Vkn : Vkn.Vkn
  {
    public override void free_programmable()
    {
      // some code
    }
  }


my plugins as vs2010 project2 or as self-compiled
namespace VknPlugin
{
  using Vkn;
  ....

  [VknAttribute("TestVkn")]
  public class Vkn : Vkn.Vkn
  {
    public override void free_programmable()
    {
      // some code
    }
  }
}

my plugins as vs2010 project3 or as self-compiled
namespace VknPlugin
{
  using Vkn;
  ....

  [VknAttribute("ABCandXYZ")]
  public class Vkn : Vkn.Vkn
  {
    public override void free_programmable()
    {
      // some code
    }
  }
}
I have also tried to gib each a unique classname ( e.g Vkn1 Vkn2 and Vkn3 ) or another namespace.


In the main app i have a handler to load the plugins
....
[ImportMany(AllowRecomposition=true)]
public List<Lazy<IVkn,IVknAttribute>> vkns {get;set;}
....
dc = new DirectoryCatalog( myPluginDir);
cc = new CompositionContainer(dc);
cb = new CompositionBatch();
cb.AddPart(this);
cb.AddPart(dc);
try
{
  cc.Compose(cb);
  foreach( var keys in vkns)
  {
    MyStringList.Add( key.Metadata.Name);
    key.Value.Init( /* some vars */)
  }

}catch ...

}

Sep 14, 2010 at 10:22 AM

I solved my problem. It was not a MEF or a CSharpCodeProvider problem but a name problem.My compiled  assemblies has all the same name because i generate them from a template-file. If i compiled them with vs2010 i used of course different names and it works.

My Resolution is that i create a unique name for each assembly and it works with the internal c# -compiler too.

Thanks to all the people who tried to help me