Skip to content

Instantly share code, notes, and snippets.

@govert
Created November 19, 2011 14:29
Show Gist options
  • Save govert/1378887 to your computer and use it in GitHub Desktop.
Save govert/1378887 to your computer and use it in GitHub Desktop.
Excel-DNA Dynamic Code from string
<DnaLibrary Name="ExcelDna Test Dynamic Method" Language="C#">
<Reference Name="System.Windows.Forms" />
<![CDATA[
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.CSharp;
using ExcelDna.Integration;
public class Test : IExcelAddIn
{
// Just to test that we are loaded.
public static double MyAdd(double d1, double d2)
{
return d1 + d2;
}
public void AutoOpen()
{
RegisterMyClass();
}
public void AutoClose()
{
}
private void RegisterMyClass()
{
string code =
@"
public class Script
{
public static double MyDynamicAdd(double d1, double d2)
{
return d1 + d2;
}
}";
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
cp.TreatWarningsAsErrors = false;
cp.ReferencedAssemblies.Add("System.dll"); //, "System.Windows.Forms.dll", "ExcelDna.Integration.dll" );
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults cr = provider.CompileAssemblyFromSource(cp, new string[] { code });
if (!cr.Errors.HasErrors)
{
Assembly asm = cr.CompiledAssembly;
Type[] types = asm.GetTypes();
List<MethodInfo> methods = new List<MethodInfo>();
// Get list of MethodInfo's from assembly for each method with ExcelFunction attribute
foreach (Type type in types)
{
foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
// foreach (Attribute attr in info.GetCustomAttributes(false))
// {
// Type attribType = attr.GetType();
// if (attribType.FullName == "ExcelDna.Integration.ExcelFunctionAttribute" ||
// attribType.FullName == "ExcelDna.Integration.ExcelCommmandAttribute" )
// {
methods.Add(info);
// }
// }
}
}
Integration.RegisterMethods(methods);
}
else
{
MessageBox.Show("Errors during compile!");
}
}
}
]]>
</DnaLibrary>
@govert
Copy link
Author

govert commented Aug 22, 2023

The problem is that the dynamically compile assembly is not being loaded into the add-in's AssemblyLoadContext (where the type resolution works correctly). It looks like the way to fix this is to set the Westwind.Scripting.CSharpScriptExecution' object's AlternateAssemblyLoadContext` property to the ALC of the add-in, like this:

            var script = new CSharpScriptExecution() { SaveGeneratedCode = true };
            script.AlternateAssemblyLoadContext = AssemblyLoadContext.GetLoadContext(this.GetType().Assembly);
            script.AddDefaultReferencesAndNamespaces();

This seems to be good enough for your examples, and you should do it even in the case where you are not referencing ExcelDna.Integration from the dynamic compiled code, since you really want the new assembly and its dependencies loaded into the right ALC where possible.

In some cases the loading of other dependencies happens only when the code runs, and that might need some places where you enter a contextual reflection scope. So you might sometimes need code like this:

using (var ctx = System.Runtime.Loader.AssemblyLoadContext.EnterContextualReflection(this.GetType().Assembly))
{
   // ... run code that loads extra assemblies here
}

Unfortunately, there is no way to make the type loading works as well under .NET 6+ as it used to be under .NET Framework where we had AppDomains to isolate the add-ins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment