LOAD NEW ASSEMBLY( EXTERNAL REFRENCE) INAPPDOMAIN( CURRENT DOMAIN )

How to load Assembly (External Refrence ) in AppDomain(Current Domain)?
Hi Sahil,
If you just need to load assembly in the application domain, you can use Assembly.LoadFrom method.
If your question is related to how to make code editor recognising types from external assembly, you need to use parser.Repository.RegisterAssemblies method like in the code below:
parser.Repository.RegisterAssemblies(GetReferencedAssemblies(assemblyName)); // assemblyName should be full assembly name including path and extension.
private string[] GetReferencedAssemblies(string assemblyName)
{
Assembly assembly = Assembly.LoadFrom(assemblyName);
AssemblyName[] result = assembly.GetReferencedAssemblies();
return result.Select(x=>x.Name).ToArray();
}
regards,
Andrew
We use the following lines:
string assemblyName = @"C:\Users\Public\Documents\AlterNET Software\Extensibility Studio v.5\Demo\Scripter\QuickStarts\CustomAssembly\ExternalAssembly.dll";
Assembly assembly1 = Assembly.LoadFrom(assemblyName);
And then we try to use AppDomain.CurrentDomain.Load(…) as in your CustomAssembly example.
But both AppDomain.CurrentDomain.Load(assemblyName) andAppDomain.CurrentDomain.Load(assembly1.FullName) fail with an example.
Hi Sahil,
Assembly.LoadFrom already loads assembly in the current application domain, if it works for your assembly, I would suggest to leave it this way - no need to call AppDomain.CurrentDomain.Load.
regards,
Andrew
Hello.
Attached you find a minimum example of what we are trying to do. But this code then shows an error that the ExternalAssembly.dll is not known to the source code.
With best regards,
Sahil Rajan
// sample code
string sampleCode = "using ExternalAssembly;" +
"public class TestClass {" +
"public void UseExternal() {" +
"CustomClass customClass = new CustomClass();" +
"customClass.TestMethod(1, true);" +
"} }";

// prepare ScriptRun
ScriptRun scriptRun = new ScriptRun();
scriptRun.ScriptLanguage = ScriptLanguage.CSharp;
scriptRun.ScriptSource.FromScriptCode(sampleCode);
scriptRun.ScriptSource.WithDefaultReferences();
scriptRun.AssemblyKind = ScriptAssemblyKind.DynamicLibrary;

// load external assembly (specified by user)
string assemblyName = @"C:\Users\Public\Documents\AlterNET Software\Extensibility Studio v.5\Demo\Scripter\QuickStarts\CustomAssembly\ExternalAssembly.dll";
Assembly assembly1 = Assembly.LoadFrom(assemblyName);

// How to make assembly1 known to scriptRun?

// compile
if (!scriptRun.Compiled) {
if (!scriptRun.Compile()) {
MessageBox.Show(scriptRun.ScriptHost.CompilerErrors[0].ToString()); // shows error that ExternalAssembly is not known.
return;
}
}
Hello Sahil,
You should add ExternalAssembly location to the ScriptSource.SearchPaths and ExternalAssembly to the ScriptSource.References like this:
string sampleCode = "using ExternalAssembly;" +
"public class TestClass {" +
"public void UseExternal() {" +
"CustomClass customClass = new CustomClass();" +
"customClass.TestMethod(1, true);" +
"} }";

// prepare ScriptRun
ScriptRun scriptRun = new ScriptRun();
scriptRun.ScriptLanguage = ScriptLanguage.CSharp;
scriptRun.ScriptSource.FromScriptCode(sampleCode);
scriptRun.ScriptSource.WithDefaultReferences();
scriptRun.AssemblyKind = ScriptAssemblyKind.DynamicLibrary;
//added code
scriptRun.ScriptSource.SearchPaths.Add(Application.StartupPath + @"\..\..""; // location of ExternalAssembly.dll
scriptRun.ScriptSource.References.Add("ExternalAssembly");
//end of added code

// load external assembly (specified by user)
string assemblyName = @"C:\Users\Public\Documents\AlterNET Software\Extensibility Studio v.5\Demo\Scripter\QuickStarts\CustomAssembly\ExternalAssembly.dll";
Assembly assembly1 = Assembly.LoadFrom(assemblyName);

// How to make assembly1 known to scriptRun?

// compile
if (!scriptRun.Compiled) {
if (!scriptRun.Compile()) {
MessageBox.Show(scriptRun.ScriptHost.CompilerErrors[0].ToString()); // shows error that ExternalAssembly is not known.
return;
}
}
regards,
Andrew
Hello Andrew,
Now the External Assembly is working .Thankyou Andrew.
What is the current version of c# and visual studio is supporting in this application?
Hello Sahil,
We support .NET Framework starting from 4.5.2 and Visual Studio 2013-2019
Installation comes with assemblies build with .NET 4.5.2, and when it comes to Roslyn-based C# parser, it supports C# 6 syntax.
We also distribute .NET 4.6 assemblies via nuget packages, these assemblies uses newer Roslyn engine and support C# 7 syntax.
Please refer to this blog for more information:
https://alternetsoft.com/blog/extensibility-studio-5-0-highlights
regards,
Andrew