mercredi 4 décembre 2019

Referencing Assembly using File Path

I am using CodeDom/Reflection to compile a C# script from a file. My current code is:

string sourceCode = File.ReadAllText(item);
var compParms = new CompilerParameters
{
    GenerateExecutable = false,
    GenerateInMemory = true
};
var csProvider = new CSharpCodeProvider();
compParms.GenerateExecutable = false;


compParms.OutputAssembly = "agame/temp/dll/common_" + Guid.NewGuid().ToString() + ".dll";

compParms.ReferencedAssemblies.Add("System.Windows.Forms.dll");
compParms.ReferencedAssemblies.Add("System.dll");
compParms.ReferencedAssemblies.Add("System.Windows.Forms.dll");
compParms.ReferencedAssemblies.Add("System.IO.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/external_acs.dll");


compParms.GenerateInMemory = true;

CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode);


object typeInstance = compilerResults.CompiledAssembly.CreateInstance("SomeClass");

MethodInfo mi = typeInstance.GetType().GetMethod("Add42");
int methodOutput = (int)mi.Invoke(typeInstance, new object[] { 1 });

MessageBox.Show(methodOutput.ToString());

An error occurs at the fourth ReferencedAssemblies.Add(), which is where I was trying to reference an external DLL.
My error was: "could not load file or assembly 'dll location' or one of its dependences. The system cannot find the file specified"
I also tried referencing all of the external DLL's dependencies, as is shown below:

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/external_acs.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-audio-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-graphics-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-network-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-system-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-window-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/libsndfile-1.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/openal32.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/Otter.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-audio-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-graphics-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-system-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-window-2.dll");

How do I reference an external DLL by the file path? If referencing it by the file path is not possible, is there a way to reference an external DLL at all? I am using it to allow scripting in my custom C# game engine.
Help with this problem would be greatly appreciated!





Aucun commentaire:

Enregistrer un commentaire