I am trying to embed C# as a scripting langague to my C++ library. I was aware that this might be not a trivial task so I went for a minimal test project to see if this is even possible first.
I have managed to create a C# assembly containing code for managing simple scripts and compiling them @ run-time using CSharpCodeProvider. The assembly was tested using C# console app as a test project (given C# source files are compiled in memory and executed when necessary).
So the next step was to bridge this with a C++ application.
I have somehow managed to do that using COM. I exported my C# dll, the classes and interfaces, as COM objects and generated the tlb file using Regasm.
I am able to create instances of these classes in C++ project and call their methods and everything seemed to work fine (for example displaying Windows.Forms message box) until the actual logic needed to be executed. It just throws exceptions that should not be thrown as It already worked with a C# console app as a host of the library.
As I don't know if I can debug COM components (well stepping into the code of this C# dll is not possible AFAIK) so I added plethora of message boxes and try-catch blocks.
It seems that it breaks when I try to retrieve any type from compiled assembly. It throws: Reflection.ReflectionTypeLoadException
The compiling on the other hand does not fail nor generate any error messages.
Using C# to trigger these methods works 100%, only while executing from C++ it does break somehow.
What might be the cause of this? Or possibly is there another way of doing what I am trying to do? I do find COM somehow right to be used in such scenario but are there aspects that I am not aware of that cause such code to break?
EDIT: This is the managed C# assembly exported as COM: http://nopaste.info/9da70dbcbd.html
So these objects are accessed from C++ app like so: http://nopaste.info/50c4c9726a.html
And the script is as simple as class deriving from IScript and implementing Execute method which displays message box.
Edit 2:
The details regarding the exception thrown is the following: "Could not load file or assembly 'lame2.scripting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=84d0df983ded76a1' or one of its dependencies. Nie można odnaleźć określonego pliku."
The last sentence means that the File could not be found.
But it is strange since when compiling I have the following line: compilerparams.ReferencedAssemblies.Add("D:\Interop\lame2.scripting\lame2.scripting\bin\Debug" + "\lame2.scripting.dll");
This is the "script" contents:
using System;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using lame2.scripting;
namespace Olaboga
{
public class TestScript : IScript
{
public TestScript() { }
public void Execute(Event ev)
{
MessageBox.Show("Script that has been compiled from a source file has been invoked. TestScript.Execute");
}
}
}
Edit 3:
Finally after switch from compilation in memory to compilation to temporary file it suddenly started to work properly. So the last question can someone knows why it failed to work while being compiled in memory and if is it possible to do so.
obj = (IScript)Activator.CreateInstance(script_type);orscript_assembly.GetTypes()?