rhughes
rhughes

Reputation: 9583

Select referenced binary at runtime

Suppose I have two versions of an assembly with the exact same public interfaces, but slightly different implementations. I reference one version during development to compile against. During my program’s startup, I would like somehow to select which one of these two assemblies to run against.

Is there a way to do this not using reflection and that is compatible with all .NET Standard 2.1 implementations?

Upvotes: 1

Views: 67

Answers (2)

sramekpete
sramekpete

Reputation: 3208

I would try to use conditions in csproj.

<ItemGroup Condition="'$(CONFIG)'=='DEBUG'>
   <ProjectReference Include="{PATH_TO_DEV_PROJECT}">
   ...
   </ProjectReference>
</ItemGroup>
<ItemGroup Condition="'$(CONFIG)'!='DEBUG'>
   <Reference Include="{PATH_TO_RELEASE_REFERENCE}" />
</ItemGroup>

That might help to copy correct assembly. You may use <PackageReference>...</PackageReference> too.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100610

Assembles are not loaded before first use - so as long as you copy the version you like to location where it will be loaded from before any types from that assembly are used it will be loaded.

Indeed using reflection (Assembly.Load/LoadFrom) is more usual solution, but it has exactly the same restriction - you must do that before any type is used, otherwise default loading logic will pick whatever is available (or simply fail).

Upvotes: 1

Related Questions