0

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.

2
  • Could you explain why you are doing this instead of a normal project reference? That will help avoid possible XY-problems. Commented Sep 2 at 9:01
  • I have core application, IPC class library and a plugin application that requests method calls (to be actually executed inside core application). Plugin references interface library and can call any method exposed by interfaces. Core application references interface library and data class library (data classes inplement that interfaces). When request result is returned to the plugin it should be matherialized to a data class (from a library which plugin doesn't reference). So I need to add this reference at runtime - either via AppDomain.CurrentDomain.AssemblyResolve or via app.config. Commented Sep 2 at 9:30

1 Answer 1

1

The document you quoted is not accurate enough. Please read this one:

If you are supplying a code base hint for an assembly that is not strong-named, the hint must point to the application base or a subdirectory of the application base directory.

Example:

<codeBase version="1.0.0.0" href="Plugins/ClassLibrary1.dll" />

Furthermore, specifying only a simple name seems insufficient for GetType to load the assembly, at least a version number is needed:

var type = Type.GetType("ClassLibrary1.Class1, ClassLibrary1, Version=1.0.0.0");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.