My solution has the following structure:
TypeLoadApp
|_TypeLoadApp.sln
|_ClassLibrary1.dll (is copied via xcopy post-build event of ClassLibrary1.csproj)
|
|_ClassLibrary1 (netstandard2.0 class library)
| |_ClassLibrary1.csproj
| |_Class1.cs
| |_bin
| |_Debug
| |_netstandard2.0 (ClassLibrary1.csproj output directory)
| |_ClassLibrary1.dll
|
|_TypeLoadApp (.Net Framework 4.7.2 console application)
|_TypeLoadApp.csproj
|_Program.cs
|_app.config
|_bin
|_Debug
|_net472 (TypeLoadApp.csproj output directory)
|_TypeLoadApp.exe
|_TypeLoadApp.exe.config
In my console application has no direct reference to my class library but loads type from it:
using System;
namespace TypeLoadApp
{
internal class Program
{
static void Main(string[] args)
{
var type = Type.GetType("ClassLibrary1.Class1, ClassLibrary1");
Console.WriteLine($"Class1 {(type == null ? "is NOT loaded" : "is loaded")}!");
Console.ReadLine();
}
}
}
I set assembly path via app.config (codeBase tag). My assembly has no PublicToken so according to MSDN:
If the assembly is a private assembly, the codebase setting must be a path relative to the application's directory.
With the following app.config is doesn't work (type is not resolved):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ClassLibrary1" />
<codeBase version="1.0.0.0" href="file:///..\..\..\..\ClassLibrary1.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I even tried direct path (e. g. file:///d:\TypeLoadAppClassLibrary1.dll) - nothing changes.
How to use App.Config codebase tag with .NET assembly relative path correctly?
My sample repo. Build ClassLibrary1 project, then build and run TypeLoadApp project.
AppDomain.CurrentDomain.AssemblyResolveor viaapp.config.